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. 跑 vue 项目

    cd ***** npm install  或 yarn(推荐) npm run dev 若是报错: missing script: dev ERR! A complete log of this r ...

  2. EasyUI实现分页、搜索、行按钮功能

    1.html+js代码: <html> <head> <meta name="viewport" content="width=device ...

  3. SpringBoot-外部运行jvm参数调优

    外部运行调优 java -server -Xms32m -Xmx32m  -jar springboot_v2.jar

  4. what's the 跳空

    出自 MBA智库百科(https://wiki.mbalib.com/) 什么是跳空 跳空在日语中叫“窗口”,英语是“gap”,又称为即价格跳空,是指汇率在快速波动时产生的曲线的最低价与曲线的最高价之 ...

  5. [js]变量提升-关于条件

    条件函数变量提示于全局中函数变量提升不一样. 条件中: 函数变量提升, 只是声明(现新版本浏览器中) if(g()){ function g() { return true } console.log ...

  6. 家庭记账本之微信小程序(四)

    json的学习 JSON:JavaScript 对象表示法(JavaScript Object Notation). JSON 是存储和交换文本信息的语法.类似 XML. JSON 比 XML 更小. ...

  7. centos7中安装、配置jdk(转载)

    参考命令:http://www.jb51.net/os/RedHat/73016.html来进行安装 安装说明 系统环境:centos7安装方式:rpm安装软件:jdk-8u25-linux-x64. ...

  8. Java基础(认识Java)

    我们要学习Java那么就要首先认识一下Java: Java是什么 Java是由Sun Microsystems公司于1995年5月推出的Java面向对象程序设计语言和Java平台的总称.由James ...

  9. Objective-C RunTime 学习笔记 之 基础结构体

    1.OC 运行期常用对象结构体 基本的结构体定义 typedef objc_class Class; /* 类 */ typedef objc_object *id; /* 各种类型,只要第一个字段为 ...

  10. IP通信基础学习第一周

    在IP方向上,IP通信基础是学习其他诸如:数字程教控交换技术与应用.IPV6技术与应用.SDH传输技术与应用等课程的基础. 学习IP通信基础,对学生而言至少可以拿学分,不仅有利于以后的学习,还有机会从 ...