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. android中include标签使用详解

    android中include标签是为了便于控件的覆用的一个很好解决方案.   但是也有一些需要注意的地方,下面是本人在项目中碰到过的一个问题,做此记录,便于以后查看.   include标签用法. ...

  2. Android中include标签的使用

    在Android的开发中,我们知道布局文件可以让我们很方便的对各个UI控件进行位置安排跟属性设置,而在程序中可以直接取得控件并赋予对应操作功能.但是,如果是一个复杂的界面设计,我们把所有布局都放在一个 ...

  3. android中include 的使用讲解

    include的作用就是重复使用同一段代码,提高代码的重用性.具体说就是,通过include 在 某布局 a.xml 中引用 B.xml布局文件,这个b.xml可同时被多个布局同时使用,所以达到了同一 ...

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

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

  5. Android中include标签的使用(打开引用布局,隐藏当前布局)

    在开发app的时候,有时候一个布局会反复用到,可以把反复用到的布局单独写一个xml文件,什么时候用到就用includ标签引入xml 下面是我写的反复用到的一个xml,里面有2个button,一个Tex ...

  6. Android中的&lt;include&gt;标签和&lt;merge&gt;标签

    android开发中经常会碰到某一个布局的复用:直接拷贝粘贴并不是是有效的策略,这时候就能够借助<include>标签和<merge>标签来完毕. 官方文档: http://d ...

  7. Android中Input型输入设备驱动原理分析(一)

    转自:http://blog.csdn.net/eilianlau/article/details/6969361 话说Android中Event输入设备驱动原理分析还不如说Linux输入子系统呢,反 ...

  8. android 中获取视频文件的缩略图(非原创)

    在android中获取视频文件的缩略图有三种方法: 1.从媒体库中查询 2. android 2.2以后使用ThumbnailUtils类获取 3.调用jni文件,实现MediaMetadataRet ...

  9. Android中NDK的搭建及简单使用 Android.mk相关介绍 JNI的使用

    Android中NDK的搭建及简单使用: 使用NDK,简述其重要步骤:.搭建NDK环境(作用:用于自动生成jni下的.c对应的so文件)---到Android NDK官网或Android官网下载ndk ...

随机推荐

  1. 11.2-uC/OS-III添加任务到就绪队列

    1.uC/OS-III提供很多服务可以把任务添加到就绪列表中. 最明显的服务是OSTaskCreate(), 它通常创建准备运行的任务并将任务放入就绪列表中.如图6-6所示,就绪列表中该优先级中已经有 ...

  2. NuGet Install-Package报错解决Package Manager Console error - PowerShell version 2.0 is not supported. Please upgrade PowerShell to 3.0 or greater and restart Visual Studio.

    问题: Package Manager Console error - PowerShell version 2.0 is not supported. Please upgrade PowerShe ...

  3. Django组件——分页器和中间件

    分页器 Django内置分页器(paginator) 分页器函数为paginator,里面有几个重要的参数需要我们了解 paginator = Paginator(book_list, 10) #第二 ...

  4. python终端打印带颜色的print

    原理 实现过程:       终端的字符颜色是用转义序列控制的,是文本模式下的系统显示功能,和具体的语言无关.       转义序列是以ESC开头,即用\033来完成(ESC的ASCII码用十进制表示 ...

  5. IntelliJ IDEA java文件注释模板

    一.设置 二.注释模板 /*** @version: java version 1.7+* @Author : * @Explain :* @contact: * @Time : ${DATE} ${ ...

  6. 记录一则xtts测试遇到的诡异现象

    背景:在一次xtts的测试中遇到因源库数据文件名称包含特殊字符导致表空间全量备份缺失文件,之所以说是诡异现象,是因为xtts的全备日志不报任何错误,在恢复阶段才发现缺少文件,这个缺陷比较隐晦,尤其在迁 ...

  7. Unity之获取资源包的路径

    先从缓存中获取,如果获取不到,则从包中获取. 如下: public static string GetAssetBundlePath(string path) { // 先尝试从 persist 目录 ...

  8. zw量化交易·实盘操作·系列培训班

    参见: <zw量化交易·实盘操作·系列培训班> http://blog.sina.com.cn/s/blog_7100d4220102w0q5.html

  9. 移动端项目在ios上输入框聚焦难解决方案

    由于引入fastclick导致ios端input.textarea输入框难以点击聚焦,解决方案如下: 找到项目中的fastclick依赖或在main.js中改写fastclick的focus实现.

  10. flutter popup

    card ? Overlay https://docs.flutter.io/flutter/widgets/Overlay-class.html pending....