网上建网站,驰易网站建设,开设一个网站的费用,wordpress 书 pdf在android布局中#xff0c;使用include#xff0c;将另一个xml文件引入#xff0c;可作为布局的一部分#xff0c;但在使用include时#xff0c;需注意以下问题#xff1a;一、使用include引入如现有标题栏布局block_header.xml#xff0c;代码如下#xff1a;Rel… 在android布局中使用include将另一个xml文件引入可作为布局的一部分但在使用include时需注意以下问题一、使用include引入如现有标题栏布局block_header.xml代码如下RelativeLayout xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:idid/layout_headerandroid:layout_widthmatch_parentandroid:layout_heightdimen/title_bar_handroid:layout_alignParentToptrueandroid:backgroundcolor/app_main_colorandroid:paddingLeftdimen/bar_pd_leftandroid:paddingRightdimen/bar_pd_leftandroid:gravitybottom ImageButtonandroid:idid/btn_backandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:srcdrawable/backandroid:backgrounddrawable/grid_item_selectorandroid:layout_alignParentLefttrueandroid:visibilityinvisible /TextView android:idid/label_titleandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:textSizedimen/title_sizeandroid:text标题栏android:textColorcolor/whiteandroid:layout_centerHorizontaltrueandroid:layout_alignBottomid/btn_backandroid:paddingBottomdimen/bar_pd_bottom/ImageButtonandroid:idid/btn_settingandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:srcdrawable/settingandroid:backgrounddrawable/grid_item_selectorandroid:layout_alignParentRighttrueandroid:visibilityinvisible //RelativeLayout现在要在activity_main.xml中引入标题栏的布局代码如下RelativeLayout xmlns:androidhttp://schemas.android.com/apk/res/androidxmlns:toolshttp://schemas.android.com/toolsandroid:layout_widthmatch_parentandroid:layout_heightmatch_parenttools:context.MainActivity includeandroid:idid/bolck_titlebarlayoutlayout/block_header /TextViewandroid:idid/textandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:textstring/hello_worldandroid:layout_belowid/bolck_titlebar //RelativeLayout二、在加载了activity_main.xml的Activity.class中对block_header.xml的控件的操作【普通控件的使用】对控件的操作和直接在activity_main中布局的控件的操作一致如设置标题栏的标题文字如下TextView tvTitle (TextView) findViewById(R.id.label_title);tvTitle.setText(“title”);【最外层的layout的使用】但要注意的是如果要对block_header.xml中最外层的布局layout_header进行操作采用RelativeLayout layoutHeader (RelativeLayout) findViewById(R.id.layout_header);获得获得到的对象为null这是由于我们为include部分设置了id属性。如果我们没有设置id属性时同样能够按照以上方式对其进行操作如我们要设置背景色没有对include设置id的做法RelativeLayout layoutHeader (RelativeLayout) findViewById(R.id.layout_header);
layoutHeader.setBackgroundColor(Color.BLUE);如果我们设置了id属性一些网页介绍通过如下方式获得并对其操作错误做法View layout getLayoutInflater().inflate(R.layout.block_header, null);
RelativeLayout layoutHeader (RelativeLayout)layout.findViewById(R.id.layout_header);
layoutHeader.setBackgroundColor(Color.BLUE);但通过实验并不能达到我们想要的效果虽然设置了背景色但是在activity_main.xml中表现出来的还是没有设置之前的样子不难解释我们通过这种方式获得的对象只是block_header.xml中的layout并不是我们include进activity_main.xml中的layout当我们在activity_main.xml设置了include的idblock_header.xml的最外层布局已被映射到include上所以只需对include的视图进行操作就相当于对block_header.xml最外层的布局进行操作具体如下对include设置了id的做法View layoutHeader findViewById(R.id.bolck_titlebar);layoutHeader.setBackgroundColor(Color.BLUE);所以在对被include的布局的最外层布局进行操作时需要特别注意如方法不正确可能会出现报空指针错误或者设置无效等问题。