zoukankan      html  css  js  c++  java
  • 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的布局的最外层布局进行操作时,需要特别注意,如方法不正确,可能会出现报空指针错误或者设置无效等问题。
  • 相关阅读:
    记忆点
    数组的操作
    console.log()中的运算与打印事件
    ie9上传后下载json
    mysql使用on duplicate key update批量更新数据
    vue 弹出菜单
    mysql备份脚本
    uniapp+nodejs微信支付小程序版
    mycat初体验
    vscode格式化html标签属性不换行(vetur插件)
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/9285071.html
Copyright © 2011-2022 走看看