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

一个系统中,往往会有重复的界面,或者被划分的可以复用的逻辑单元,这个时候的解决方法往往是,做好一个之后,然后进行Copy。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下面。但是如果给inlclude设置了id标签,那么上面的代码就运行失败。

<!-- include标签内设置id属性后(android:id),其引用的布局layout内的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);

  

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

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

最新文章

  1. 【iOS [[UIApplication sharedApplication] delegate]】运用
  2. Microsoft.CompactFramework.CSharp.targets not found
  3. Java中有四种常见的Map实现方法
  4. java中有关线程的题目
  5. 利用Roslyn构建一个简单的C#交互脚本引擎
  6. EWM ODO清理功能
  7. axel源码学习(1)&mdash;&mdash;重要流程细节
  8. PHPUML 生成UML
  9. vue2.0 组件通信
  10. 深入Java虚拟机(3)——安全
  11. Failed to execute goal org.apache.tomcat.maven:tomcat7-maven-plugin:2.2:deploy (default-cli) on project Resource: Cannot invoke Tomcat manager: Connection refused: connect -&gt; [Help 1]
  12. Java 连接 SqlServer工具类
  13. 设置pip代理
  14. echarts 怎样去掉白色边框线 和怎样去除背景中的网格
  15. WP8.1学习系列(第二章)——Toast通知
  16. PHP安装posix、pctl扩展
  17. iOS开源项目:AFNetworking----写得非常好
  18. Eclipse导入web项目发布项目时报Tomcat version 7.0 only supports J2EE 1.2, 1.3, 1.4, and Java EE 5 and 6 Web错误解决方案
  19. 10个基于 JavaScript 的 WYSIWYG 编辑器
  20. 【BZOJ1005/1211】[HNOI2008]明明的烦恼/[HNOI2004]树的计数 Prufer序列+高精度

热门文章

  1. class_create(),device_create()使用
  2. css 动画效果
  3. 【nodejs】 npm 注意事项
  4. mac os快捷键
  5. Oracle 多行记录合并/连接/聚合字符串的几种方法
  6. sqlserver convert 日期时间 转换格式化
  7. NOI2014 全国互测Round2
  8. Win7超级终端查看单片机printf输出
  9. struts2多文件上传(带进度条)demo+说明
  10. JSP/SERVLET重定向技术综述