android中include标签

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);

相关推荐

  1. androidinclude标签

    2024-04-01 10:48:05       35 阅读
  2. 【mybatis <sql>,<include标签

    2024-04-01 10:48:05       53 阅读
  3. android include 和 merge 区别

    2024-04-01 10:48:05       23 阅读
  4. C语言 #include<>与 include ““的区别

    2024-04-01 10:48:05       34 阅读
  5. PHPinclude 和 require 语句

    2024-04-01 10:48:05       58 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-01 10:48:05       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-01 10:48:05       101 阅读
  3. 在Django里面运行非项目文件

    2024-04-01 10:48:05       82 阅读
  4. Python语言-面向对象

    2024-04-01 10:48:05       91 阅读

热门阅读

  1. 【Go】面向萌新的Gin框架知识梳理学习笔记

    2024-04-01 10:48:05       30 阅读
  2. StatefulSet介绍-更新-扩容缩容-HPA

    2024-04-01 10:48:05       27 阅读
  3. 2024.3.31力扣(1200-1400)刷题记录

    2024-04-01 10:48:05       37 阅读
  4. 著名的分布式数据库

    2024-04-01 10:48:05       30 阅读
  5. 从适用场景看,Spring Boot和Spring的不同

    2024-04-01 10:48:05       38 阅读
  6. Servlet

    Servlet

    2024-04-01 10:48:05      32 阅读
  7. Spring Boot集成Elasticsearch 8.12.2客户端

    2024-04-01 10:48:05       35 阅读
  8. ZooKeeper 负载均衡和 Nginx 负载均衡的区别

    2024-04-01 10:48:05       36 阅读
  9. Docker Swarm入门

    2024-04-01 10:48:05       38 阅读
  10. Redis 的常见问题及解决方案

    2024-04-01 10:48:05       43 阅读