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. php 与mysql 数据库

    新手上路,php刚学,数据库刚学,花了两天终于读到数据库的数据了. 一路上的坎坷我在这里吐槽吐槽: <?php $mysql_server_name='localhost'; //改成自己的my ...

  2. Grafana 安装及 Windows 应用程序服务配置工具 NSSM使用

    网址:https://blog.csdn.net/kk185800961/article/details/83515382 1.进入conf文件,将 defaults.ini 复制一份,命名为cust ...

  3. Centos下,Docker部署Yapi接口管理平台(详细得令人发指)

    接口测试的工具很多,公司引进了接口管理平台Yapi,自己尝试直接搭建,从安装Nodejs到配置MongoDB数据库,再到安装yapi的时候,遇到浏览器打开本地服务器Ip地址后,没有显示部署内容...没 ...

  4. JDK 1.8源码阅读 LinkList

    一,前言 LinkedList是一个实现了List接口和Deque接口的双端链表.有关索引的操作可能从链表头开始遍历到链表尾部,也可能从尾部遍历到链表头部,这取决于看索引更靠近哪一端. LinkedL ...

  5. JAVA:IDEA安装、创建项目、tomcat配置、maven配置(1)

    一.安装 JDK安装自行百度,IDEA安装: IDEA安装包下载:https://www.jetbrains.com/idea/download/ 安装过程一路默认,需要选择的就勾选 .java之类的 ...

  6. 构建高性能的MYSQL数据库系统-主从复制

    实验环境: DB1:172.16.1.100 DB2:172.16.1.101 VRRIP:172.16.1.99 步骤: yum -y install mysql 1.修改DB1的mysql配置文件 ...

  7. ASP.net MVC5 Code First填充测试数据到数据库

    问题的产生  最近在看Adam Freeman的“Pro ASP.NET MVC5”,于是在工作机上面搭建了相应的运行环境,但是在自己的机器上面只有代码,没有数据库.记得在code first中可以新 ...

  8. centos7安装redist 以及redis扩展

    wget http://download.redis.io/releases/redis-3.2.1.tar.gz   用wget下载 $ tar xzf redis-3.2.1.tar.gz   解 ...

  9. IDEA 自动重新载入

    IDEA 自动重新载入: Ctrl + F9

  10. 【css】适配iphoneX

    /*适配iphoneX*/ @media only screen and (device-width: 375px) and (device-height: 812px) and (-webkit-d ...