android布局中使用include及需注意点

转载:https://blog.csdn.net/u013524014/article/details/52240277


在android布局中,使用include,将另一个xml文件引入,可作为布局的一部分,但在使用include时,需注意以下问题:


一、使用include引入

如现有标题栏布局block_header.xml,代码如下:

[html]  view plain  copy
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.         android:id="@+id/layout_header"  
  3.         android:layout_width="match_parent"  
  4.         android:layout_height="@dimen/title_bar_h"  
  5.         android:layout_alignParentTop="true"  
  6.         android:background="@color/app_main_color"  
  7.         android:paddingLeft="@dimen/bar_pd_left"  
  8.         android:paddingRight="@dimen/bar_pd_left"  
  9.         android:gravity="bottom" >  
  10.         <ImageButton  
  11.             android:id="@+id/btn_back"  
  12.             android:layout_width="wrap_content"  
  13.             android:layout_height="wrap_content"  
  14.             android:src="@drawable/back"  
  15.             android:background="@drawable/grid_item_selector"  
  16.             android:layout_alignParentLeft="true"  
  17.             android:visibility="invisible" />  
  18.               
  19.         <TextView android:id="@+id/label_title"  
  20.             android:layout_width="wrap_content"  
  21.             android:layout_height="wrap_content"  
  22.             android:textSize="@dimen/title_size"  
  23.             android:text="标题栏"  
  24.             android:textColor="@color/white"  
  25.             android:layout_centerHorizontal="true"  
  26.             android:layout_alignBottom="@id/btn_back"  
  27.             android:paddingBottom="@dimen/bar_pd_bottom"/>  
  28.           
  29.         <ImageButton  
  30.             android:id="@+id/btn_setting"  
  31.             android:layout_width="wrap_content"  
  32.             android:layout_height="wrap_content"  
  33.             android:src="@drawable/setting"  
  34.             android:background="@drawable/grid_item_selector"  
  35.             android:layout_alignParentRight="true"  
  36.             android:visibility="invisible" />  
  37. </RelativeLayout>  

现在要在activity_main.xml中引入标题栏的布局,代码如下:

[html]  view plain  copy
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.     <include  
  7.         android:id="@+id/bolck_titlebar"  
  8.         layout="@layout/block_header" />  
  9.       
  10.   
  11.     <TextView  
  12.         android:id="@+id/text"  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:text="@string/hello_world"  
  16.         android:layout_below="@id/bolck_titlebar" />  
  17.   
  18. </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的id,block_header.xml的最外层布局已被映射到include上,所以只需对include的视图进行操作,就相当于对block_header.xml最外层的布局进行操作

具体如下(对include设置了id的做法):

View layoutHeader = findViewById(R.id.bolck_titlebar);

layoutHeader.setBackgroundColor(Color.BLUE);



所以在对被include的布局的最外层布局进行操作时,需要特别注意,如方法不正确,可能会出现报空指针错误或者设置无效等问题

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值