android中include标签是为了便于控件的覆用的一个很好解决方案。

但是也有一些需要注意的地方,下面是本人在项目中碰到过的一个问题,做此记录,便于以后查看。

include标签用法。

1.新建一个xml文件,命名 head.xml
head.xml文件内容如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/index_linear_foot"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
        <ImageView
            android:id="@+id/head_btn_refresh"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
        />
</RelativeLayout>
2.新建一个布局文件,命名 main.xml
main.xml文件内容如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
>
<include android:layout_width="fill_parent" android:layout_height="wrap_content" layout="@layout/head" />
</LinearLayout>
注意:上面我们的include标签中是没有为它指定id的。

3.新建一个MainActivity,设置布局文件为main.xml;

4.假设我现在需要在代码中为head.xml中的RelativeLayout容器设置背景图片。
代码如下:
//获得布局容器对象
RelativeLayout head = (RelativeLayout)findViewById(R.id.index_linear_foot);
//设置背景图片
head.setBackgroundResource(R.drawable.head);
这样就OK了。

5.刚刚说到,我们的include标签中是没有为它指定id的,假设我们现在的main.xml文件布局容器是RelativeLayout,而我需要把某个控件放在head.xml下面。就需要使用到RelativeLayout布局容器的特有属性 android:layout_below="" 属性。还需要为include指定id属性。
那我们的main.xml文件变成如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<include android:id="@+id/main_head" android:layout_width="fill_parent" android:layout_height="wrap_content" layout="@layout/head" />
<ListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/main_headb"
/>
</RelativeLayout>
那接下来我们在运行我们的实例,结果发现,代码在运行到head.setBackgroundResource(R.drawable.head);
这一句的时候抛异常了
java.lang.NullPointerException

原来:如果include指定了id的话,就不能直接把它里面的控件当成主xml中的控件来直接获得了,必须先获得这个xml布局文件,再通过布局文件findViewById来获得其子控件。
代码如下
View layout = getLayoutInflater().inflate(R.layout.head, null);
RelativeLayout head= (RelativeLayout)layout.findViewById(R.id.index_linear_foot);
//设置背景图片
head.setBackgroundResource(R.drawable.head);

转载于:https://www.cnblogs.com/xyzlmn/archive/2012/01/12/3168132.html

Android include 标签相关推荐

  1. Android include 标签使用

    include 标签就是在一个布局中引入另一个布局, 好处呢,可能就是有多个界面某个部位的内容相同单独写了一个布局,在使用的时候使用include 还有可能界面布局在一个布局太长了,就多写了几个布局 ...

  2. android中include标签的使用

    在一个项目中我们可能会需要用到相同的布局设计,如果都写在一个xml文件中,代码显得很冗余,并且可读性也很差,所以我们可以把相同布局的代码单独写成一个模块,然后用到的时候可以通过<include ...

  3. Android: Custom View和include标签的区别

    Custom View, 使用的时候是这样的: <com.example.home.alltest.view.MyCustomViewandroid:id="@+id/customVi ...

  4. Android优化——UI优化(二) 使用include标签复用布局

    使用include标签复用布局 - 1.include标签的作用 假如说我下图的这个布局在很多界面都用到了,我该怎么办?每个页面都写一遍的话,代码太冗余,并且维护难度加大. <LinearLay ...

  5. android include 点击事件,Android 多个include标签的监听事件处理

    include标签的作用是为了xml文件代码的模块化,详细不再多提.主要是说说include标签的监听. 网上也有很多例子,不过大多是只写了一个include标签的监听,如果需要实现多个include ...

  6. [转载]Android Layout标签之-viewStub,requestFocus,merge,include

    定义Android Layout(XML)时,有四个比较特别的标签是非常重要的,其中有三个是与资源复用有关,分别是<viewStub/>, <requestFocus />, ...

  7. Android中View绘制优化二一---- 使用include /标签复用布局文件

    本文原创, 转载请注明出处:http://blog.csdn.net/qinjuning 译二: 使用<include />标签复用布局文件  翻译地址:http://developer. ...

  8. Android Layout标签之-viewStub,requestFocus,merge,include

    定义Android Layout(XML)时,有四个比较特别的标签是非常重要的,其中有三个是与资源复用有关,分别是<viewStub/>, <requestFocus />, ...

  9. Android vector标签 PathData 画图超详解

    此文章来源于https://www.cnblogs.com/yuhanghzsd/p/5466846.html点击打开链接 Android vector标签 PathData 画图超详解 SVG是一种 ...

最新文章

  1. Struts 2的文件上传
  2. 原子操作之sync/atomic
  3. ios 设置属性的center_ios-坐标系统(详解UIView的frame、bounds跟center属性[图])
  4. 拥有成本分析:Oracle WebLogic Server与JBoss
  5. 简易航空订票系统_四川航空APP全新升级改版,三大亮点提升订票体验
  6. Java 实现Https访问工具类 跳过ssl证书验证
  7. 微信小程序setData的回调方法
  8. windows下为eclipse配置pydev
  9. php qq邮箱发送邮件报错_PHP实现发送邮件(二)
  10. android edittext最多输入,android 中如何限制 EditText 最大输入字符数
  11. Facebook的体系结构分析---外文转载
  12. 今天 Java 14 正式发布了!放弃 Java 8 行吗?
  13. 学习第四章与第五章的感悟
  14. cad断点快捷键_CAD命令:打断(BREAK)命令的使用技巧
  15. Driverjs-前端引导页组件
  16. a[i]-‘0‘与(int)a[i]区别
  17. U-BOOT启动流程详细分析[转]
  18. coreldraw凹槽_Coreldraw教程:打造逼真静物写生
  19. 如何查询大量DHL国际快递物流轨迹、签收情况
  20. ^(?:([^,]*),?){1} 的含义

热门文章

  1. android点击失效,android点击无效验证的解决方法
  2. 大数据概念思维导图_思维导图|数据化风控(信用评分建模教程)
  3. 蔡明机器人对比_“百变”蔡明:一个拥有性感身材的“小品天后”!
  4. dell 服务器系统6,dell C6100服务器安装windows2008R2系统
  5. 力扣(LeetCode)刷题,简单题(第16期)
  6. 计算并显示HOG直方图
  7. mask rcnn网络结构笔记
  8. php与c 哪个好,C语言和PHP,新手选择哪个比较好?
  9. R6010-abort() has been called,Error openning file ../../modules/highgui/src/cap_ffmpeg_impl.hpp解决方法
  10. Python中if__name__==__main__:该如何理解