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

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

一、使用include引入

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
        android:id="@+id/layout_header"  
        android:layout_width="match_parent"  
        android:layout_height="@dimen/title_bar_h"  
        android:layout_alignParentTop="true"  
        android:background="@color/app_main_color"  
        android:paddingLeft="@dimen/bar_pd_left"  
        android:paddingRight="@dimen/bar_pd_left"  
        android:gravity="bottom" >  
        <ImageButton  
            android:id="@+id/btn_back"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:src="@drawable/back"  
            android:background="@drawable/grid_item_selector"  
            android:layout_alignParentLeft="true"  
            android:visibility="invisible" />  

        <TextView android:id="@+id/label_title"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:textSize="@dimen/title_size"  
            android:text="标题栏"  
            android:textColor="@color/white"  
            android:layout_centerHorizontal="true"  
            android:layout_alignBottom="@id/btn_back"  
            android:paddingBottom="@dimen/bar_pd_bottom"/>  

        <ImageButton  
            android:id="@+id/btn_setting"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:src="@drawable/setting"  
            android:background="@drawable/grid_item_selector"  
            android:layout_alignParentRight="true"  
            android:visibility="invisible" />  
</RelativeLayout>  

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    tools:context=".MainActivity" >  
    <include  
        android:id="@+id/bolck_titlebar"  
        layout="@layout/block_header" />  


    <TextView  
        android:id="@+id/text"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="@string/hello_world"  
        android:layout_below="@id/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的id,block_header.xml的最外层布局已被映射到include上,所以只需对include的视图进行操作,就相当于对block_header.xml最外层的布局进行操作

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

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

layoutHeader.setBackgroundColor(Color.BLUE);

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值