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);
这样就可以了。

android中include标签使用详解的更多相关文章

  1. Drawable实战解析:Android XML shape 标签使用详解(apk瘦身,减少内存好帮手)

    Android XML shape 标签使用详解   一个android开发者肯定懂得使用 xml 定义一个 Drawable,比如定义一个 rect 或者 circle 作为一个 View 的背景. ...

  2. Android XML shape 标签使用详解(apk瘦身,减少内存好帮手)

    Android XML shape 标签使用详解   一个android开发者肯定懂得使用 xml 定义一个 Drawable,比如定义一个 rect 或者 circle 作为一个 View 的背景. ...

  3. Android中Service的使用详解和注意点(LocalService)

    Android中Service的使用详解和注意点(LocalService) 原文地址 开始,先稍稍讲一点android中Service的概念和用途吧~ Service分为本地服务(LocalServ ...

  4. Android中SurfaceView的使用详解

    Android中SurfaceView的使用详解 http://blog.csdn.net/listening_music/article/details/6860786 Android NDK开发 ...

  5. Android中的windowSoftInputMode属性详解

    这篇文章主要介绍了Android中的windowSoftInputMode属性详解,本文对windowSoftInputMode的9个属性做了详细总结,需要的朋友可以参考下     在前面的一篇文章中 ...

  6. Android中Canvas绘图基础详解(附源码下载) (转)

    Android中Canvas绘图基础详解(附源码下载) 原文链接  http://blog.csdn.net/iispring/article/details/49770651   AndroidCa ...

  7. Android中Application类的详解:

    Android中Application类的详解: 我们在平时的开发中,有时候可能会须要一些全局数据.来让应用中的全部Activity和View都能訪问到.大家在遇到这样的情况时,可能首先会想到自定义一 ...

  8. 【Android 界面效果25】android中include标签的使用

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

  9. Android 中的消息传递,详解广播机制

    --------------------------------------广播机制简介--------------------------------------------- Android中的广 ...

随机推荐

  1. jquery-1.11.1.js

       每次想要使用这个js时,总是要到官网上下载,太麻烦,现在把它收录了 jquery-1.11.1.js /*! * jQuery JavaScript Library v1.11.1 * http ...

  2. DirectShow中写push模式的source filter流程 + 源代码(内附详细注释)

    虽然网上已有很多关于DirectShow写source filter的资料,不过很多刚开始学的朋友总说讲的不是很清楚(可能其中作者省略了许多他认为简 单的过程),读者总希望看到象第一步怎么做,第二步怎 ...

  3. 芝麻HTTP:非关系型数据库存储

    NoSQL,全称Not Only SQL,意为不仅仅是SQL,泛指非关系型数据库.NoSQL是基于键值对的,而且不需要经过SQL层的解析,数据之间没有耦合性,性能非常高. 非关系型数据库又可细分如下. ...

  4. SQLite笔记

    一.SQLite下载: http://www.sqlite.org/download.html (或在NuGet下载安装) 二.SQLite操作: 1.添加引用System.Data.SQLite,如 ...

  5. 2016弱校联盟十一专场10.3 We don't wanna work!

    能把 not working now 写成 not working hard now 还查一晚上也是没谁了 我的做法是维护两个set 分别是前20% 和后80% #include<iostrea ...

  6. VMware下载安装及CentOS7下载安装

    我是在Windows10系统下,下载的VMware Workstation 12 1.下载虚拟机软件 安装包位置(里面有激活码):https://pan.baidu.com/s/1i5hn5lj 2. ...

  7. (luogu P4012)深海机器人问题 [TPLY]

    网页链接 https://www.luogu.org/problemnew/show/4012 做题背景 在不久的将来,人工智能发展使得人类大量失业,也使得现在的我们做[深海机器人问题]做得想死... ...

  8. [BZOJ4195] [NOI2015] 程序自动分析 (并查集)

    Description 在实现程序自动分析的过程中,常常需要判定一些约束条件是否能被同时满足. 考虑一个约束满足问题的简化版本:假设x1,x2,x3,…代表程序中出现的变量,给定n个形如xi=xj或x ...

  9. less-more使用方法及区别

    Less按屏(空格,page up/page down)或按行(回车)查看文件 Less按屏(空格)或按行(回车)查看文件(不能向上翻)

  10. java支付宝开发-01-沙箱环境接入

    一.沙箱环境说明 (1)蚂蚁沙箱环境(Beta)是协助开发者进行接口功能开发及主要功能联调的辅助环境.沙箱环境模拟了开放平台部分产品的主要功能和主要逻辑(当前沙箱支持产品请参考“沙箱支持产品列表”). ...