编程的世界有的时候很微妙,有的时候就好像是在解决一个哲学问题,Android开发的时候,所有的布局,颜色,等(其实这些都可以称之为资源,Android中的资源是指非代码部分,如图片、音频、视频、字符等资源,其实就是可以被代码所操作的一些对象)都可以用XML文件布局,而所有对这些XML的操作可以在相应的Activity中进行,这种分离似乎将程序员和美工进行分离。

一个系统中,往往会有重复的界面,或者被划分的可以复用的逻辑单元,这个时候的解决方法往往是,做好一个之后,然后进行Copy。android中提供了一个include标签也可以用来解决复用问题。

  1. include标签用法。
  2.  
  3. 1.新建一个xml文件,命名 head.xml
  4. head.xml文件内容如下:
  5. <?xml version="1.0" encoding="utf-8"?>
  6. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  7. android:id="@+id/index_linear_foot"
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:orientation="horizontal"
  11. >
  12. <ImageView
  13. android:id="@+id/head_btn_refresh"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. />
  17. </RelativeLayout>
  18. 2.新建一个布局文件,命名 main.xml
  19. main.xml文件内容如下:
  20. <?xml version="1.0" encoding="utf-8"?>
  21. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  22. android:layout_width="fill_parent"
  23. android:layout_height="fill_parent"
  24. android:orientation="vertical"
  25. >
  26. <include android:layout_width="fill_parent" android:layout_height="wrap_content" layout="@layout/head" />
  27. </LinearLayout>
  28. 注意:上面我们的include标签中是没有为它指定id的。
  29.  
  30. 3.新建一个MainActivity,设置布局文件为main.xml;
  31.  
  32. 4.假设我现在需要在代码中为head.xml中的RelativeLayout容器设置背景图片。
  33. 代码如下:
    //获得布局容器对象
  34. RelativeLayout head = (RelativeLayout)findViewById(R.id.index_linear_foot);
  35. //设置背景图片
  36. head.setBackgroundResource(R.drawable.head);
  37. 这样就OK了。
  38.  
  39. 5.上面说过,我们的include标签中是没有为它指定id的,假设我们现在的main.xml文件布局容器是RelativeLayout,而我需要把某个控件放在head.xml下面。但是如果给inlclude设置了id标签,那么上面的代码就运行失败。

<!-- include标签内设置id属性后(android:id),其引用的布局layout内的id属性就不起作用了,需要先获取该布局文件,然后通过该布局文件获取里面的对象-->

  1. main.xml文件变成如下:
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <include android:id="@+id/main_head" android:layout_width="fill_parent" android:layout_height="wrap_content" layout="@layout/head" />
  8. <ListView
  9. android:id="@+id/listview"
  10. android:layout_width="fill_parent"
  11. android:layout_height="wrap_content"
  12. android:layout_below="@+id/main_headb"
  13. />
  14. </RelativeLayout>
  15. 那接下来我们在运行我们的实例,结果发现,代码在运行到head.setBackgroundResource(R.drawable.head);
  16. 这一句的时候抛异常了
  17. java.lang.NullPointerException
  18.  
  19. 原来:如果include指定了id的话,就不能直接把它里面的控件当成主xml中的控件来直接获得了,必须先获得这个xml布局文件,再通过布局文件findViewById来获得其子控件。
  20. 代码如下。
  21. View layout = getLayoutInflater().inflate(R.layout.head, null);
  22. RelativeLayout head= (RelativeLayout)layout.findViewById(R.id.index_linear_foot);
  23. //设置背景图片
  24. head.setBackgroundResource(R.drawable.head);

  

  1. 在使用了Relativelayout布局,并在该布局中用到了include标签如果没有同时重载layout_widthlayout_height属性,
    其它的layout_*属性会被忽略掉,除非同时重载了这两个属性。
    还有一种方法就是利用LinearLayout包装下。

  参考 :http://blog.csdn.net/race604/article/details/7564088

Android Include标签的更多相关文章

  1. android include标签的使用,在RelativeLayout中使用include标签需注意!!!!!

    转:http://4265337.blog.163.com/blog/static/195375820127935731114/ include和merge标记的作用主要是为了解决layout的重用问 ...

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

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

  3. Android 多个include标签的监听事件处理

    include标签的作用是为了xml文件代码的模块化,详细不再多提.主要是说说include标签的监听. 网上也有很多例子,不过大多是只写了一个include标签的监听,如果需要实现多个include ...

  4. Android优化——UI优化(二) 使用include标签复用布局

    使用include标签复用布局 - 1.include标签的作用 假如说我下图的这个布局在很多界面都用到了,我该怎么办?每个页面都写一遍的话,代码太冗余,并且维护难度加大. <LinearLay ...

  5. Android include和merge标签的使用

    在Android布局文件中,某些时候使用include标签会很多的好处1,对于稍微有点复杂的布局界面,将所有布局代码都写在一个xml文件中,界面会显得很冗余,可读性很差,这时可以分开使用include ...

  6. Android中View绘制优化二一---- 使用<include />标签复用布局文件

    本文原创, 转载请注明出处:http://blog.csdn.net/qinjuning   译二:   使用<include />标签复用布局文件      翻译地址:http://de ...

  7. Android里merge和include标签的使用

    1.使用<include /> 标签来重用layout代码 如果在一个项目中需要用到相同的布局设计,可以通过<include /> 标签来重用layout代码,该标签在andr ...

  8. android中include标签使用详解

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

  9. Android: Custom View和include标签的区别

    Custom View, 使用的时候是这样的: <com.example.home.alltest.view.MyCustomView android:id="@+id/customV ...

随机推荐

  1. 1066. Root of AVL Tree (25)

    An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...

  2. Windows Phone中的图形渲染处理

    前言    这篇文章主要介绍Windows Phone中XAML的渲染处理过程,你可以根据下文中所提到的内容来优化现有的XAML页面,来提高页面渲染的性能. XAML的渲染包括以下两个阶段: 1. 光 ...

  3. SkyDrive Pro client now available as standalone download. Hurray!

    SkyDrive Pro client now available as standalone download. Hurray! by  Todd O. Klindt  on 5/21/2013 1 ...

  4. JSP/SERVLET重定向技术综述

    1.RequestDispatcher.forward() 是在服务器端起作用,当使用forward()时,Servlet engine传递HTTP请求从当前的Servlet or JSP到另外一个S ...

  5. [转载]C# winform登陆框验证码的实现

    验证码技术已愈来愈成熟,从最初的数字.字母.字符.汉字已经到目前的语言,其应用也甚广,之前大多数只有在网站上可以看到,现在在一些客户端软件也经常可见(比如证券相关软件).之前做的一个基于 C# 客户端 ...

  6. MongoDB { code: 18, ok: 0.0, errmsg: "auth fails" } 原因

    MongoDB出现 { code: 18, ok: 0.0, errmsg: "auth fails" }  错误的原因: 1.账号密码错误 2.账号不属于该数据库

  7. [设计模式] 10 外观模式 facade

    外观模式应该是用的很多的一种模式,特别是当一个系统很复杂时,系统提供给客户的是一个简单的对外接口,而把里面复杂的结构都封装了起来.客户只需使用这些简单接口就能使用这个系统,而不需要关注内部复杂的结构. ...

  8. hdu 1166 树状数组 线段树入门

    点修改 区间求和 #include <cstdio> #include <cstdlib> #include <cmath> #include <map> ...

  9. uva 11076

    计算出每一位上数字i会出现的次数  累加 #include <cstdio> #include <cstdlib> #include <cmath> #includ ...

  10. java基础知识回顾之javaIO类--File类应用:删除带内容的目录

    /** * 深度删除一个带内容的目录 * 原理:必须从里往外删除,需要深度遍历 * @author Administrator * */ public class FileDeleteList { / ...