在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" ><ImageButtonandroid: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"/><ImageButtonandroid: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" ><includeandroid:id="@+id/bolck_titlebar"layout="@layout/block_header" /><TextViewandroid: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的布局的最外层布局进行操作时,需要特别注意,如方法不正确,可能会出现报空指针错误或者设置无效等问题。

android布局中使用include及需注意点相关推荐

  1. 在android布局中使用include和merge标签

    在我们开发Android布局时,经常会有很多的布局是相同的,这个时候我们可以通过<include/>和<merge/>标签实现将复杂的布局包含在需要的布局中,减少重复代码的编写 ...

  2. Android布局文件之 include 详细介绍

    Android布局文件之 include 详细介绍 include简介 众所周知,include就是在一个布局中,导入另一个布局文件. 优势是:相同的页面只需写一次,提高了共通布局的复用性. 下面我们 ...

  3. android 布局 站位符,基于android布局中的常用占位符介绍

    大家在做布局文件是肯定会遇到过下面的这种情况 填充出现问题,所以需要用到占位符规范填充 汉字常用占位符: android:layout_width="wrap_content" a ...

  4. Android布局中的空格以及占一个汉字宽度的空格的实现

    在Android布局中进行使用到空格,以便实现文字的对齐.那么在Android中如何表示一个空格呢? 空格: 窄空格:  一个汉字宽度的空格:   [用两个空格(  )占一个汉字的宽度时,两个空格比一 ...

  5. android 多个占位符,基于android布局中的常用占位符介绍

    大家在做布局文件是肯定会遇到过下面的这种情况 填充出现问题,所以需要用到占位符规范填充 汉字常用占位符: android:layout_width="wrap_content" a ...

  6. 详解Android布局中gravity与layout_gravity属性

    在android布局中,我们经常会用到"重心"-gravity这个属性.但是gravity有不同的类型: gravity layout_gravity 相对布局中的layout_c ...

  7. Android布局中margin与padding的区别

    我们知道Android开发不仅仅有代码的动态控制,而且还涉及到布局的静态搭建(xml).几乎在每一个xml文件中,我们总会看到margin和padding这两个属性,今天让我们初步探究一下它们之间的区 ...

  8. android layout include merge,Android 布局优化之include与merge

    Android 官方提供了三个用来优化布局的标签,分别是include.merge与ViewStub,其中ViewStub是动态加载视图到内存,大家可以查阅:Android UI布局优化之ViewSt ...

  9. android 布局中 layout_gravity、gravity、orientation、layout_weight

    线性布局中,有 4 个及其重要的参数,直接决定元素的布局和位置,这四个参数是 android:layout_gravity ( 是本元素相对于父元素的重力方向 ) android:gravity (是 ...

最新文章

  1. linux安装zookeeper+使用命令
  2. cuba 平台_认识CLI for CUBA平台
  3. [19/03/16-星期六] 常用类_Date时间类DateFormat类
  4. 终于,我读懂了所有Java集合——sort
  5. oracle数据库pfile文件,Oracle pfile/spfile参数文件详解
  6. java cmd 进程_关闭CMD进程-JAVA
  7. Ubuntu 软件包管理 常用 apt 命令
  8. Mongoose Schema hasn't been registered for model
  9. 如何调整c盘分区大小,怎样把c盘空间调整小些
  10. 使用 Pixel 3 的 Photobooth 自动拍摄最美自拍照
  11. 面试时,如何正确介绍自己的项目经验?
  12. 如何用CSS实现div元素高度相对于整个屏幕100%
  13. 原价游戏太贵?爬取steam游戏优惠信息
  14. NNNNNNNNNN
  15. Kali安装GVM(openvas)教程并更改用户密码
  16. 【20220629】【信号处理】(平稳随机信号)自相关函数性质的证明过程
  17. vue项目中引入Luckysheet
  18. 剧本翻译之SHUFFLE 6月21日
  19. ExecuteScaler的三种返回值
  20. AOP机制之环绕通知的见解

热门文章

  1. 怎样提高WebService性能大数据量网络传输处理(转)
  2. leetcode976. 三角形的最大周长(又是你得不到的简单题)
  3. STL源码剖析 基本算法 equal | fill | iter_awap | lexicographical_compare | max | min | swap |mismatch
  4. STL源码剖析 数值算法 accumulate | adjacent_difference | inner_product | partial_sum | power | itoa
  5. 哈希表和有序表的简单介绍
  6. 新手必看一位老司机的十年开车经验
  7. 科目三电子考的通过率普遍偏低
  8. 金山网络CEO傅盛:简约之美
  9. 面试风云录(02) - 与顶级CTO交手的难忘经验...
  10. 记录 Parameter with that position [1] did not exist; nested exception is java.lang.IllegalArgumentExce