Android在xml文件中可使用include包含其他定义好的布局, 可以将多处用到的布局单独出来,然后用include包含进来,这种包含方法相当于把原来布局的一部分代码独立出来,供大家共同使用,也就相当于面向对向中的类的概念差不多。下面我们逐步讲解include的作用。

先看下我们要实现的整体界面:

一、未使用Include时

通常情况下,我们直接就能写出布局代码,下面是所使用的XML代码:

[html] view plain copy 
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical" >
  6. <!-- 第一部分 -->
  7. <TextView
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:background="#ff0000"
  11. android:text="第一个BTN" />
  12. <Button
  13. android:id="@+id/mybutton"
  14. android:layout_width="fill_parent"
  15. android:layout_height="wrap_content"
  16. android:text=" One Button " />
  17. <!-- 第二部分 -->
  18. <TextView
  19. android:layout_width="fill_parent"
  20. android:layout_height="wrap_content"
  21. android:background="#00ff00"
  22. android:text="第二个BTN" />
  23. <Button
  24. android:id="@+id/mybutton"
  25. android:layout_width="fill_parent"
  26. android:layout_height="wrap_content"
  27. android:text=" Second Button " />
  28. <!-- 最后的按钮 -->
  29. <Button
  30. android:id="@+id/another"
  31. android:layout_width="wrap_content"
  32. android:layout_height="wrap_content"
  33. android:text=" Another Button " />
  34. </LinearLayout>

这段代码理解起来一点难度没有,就是几个TextView和几个Button,下面我们用include把这段代码给分割成几个文件,并完成相同的效果;

二、使用Include时

1、先将上面代码标记有“第一部分”的,代码段分离成一个文件(sublayout1.xml);

[html] view plain copy 
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="wrap_content"
  5. android:background="#505050"
  6. android:orientation="vertical" >
  7. <TextView
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:background="#ff0000"
  11. android:text="第一个BTN" />
  12. <Button
  13. android:id="@+id/mybutton"
  14. android:layout_width="fill_parent"
  15. android:layout_height="wrap_content"
  16. android:text=" One Button " />
  17. </LinearLayout>

2、再将标记有“第二部分”的代码段,分离成第二个文件(sublayout2.xml):

[html] view plain copy 
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="wrap_content"
  5. android:orientation="vertical" >
  6. <TextView
  7. android:layout_width="fill_parent"
  8. android:layout_height="wrap_content"
  9. android:background="#00ff00"
  10. android:text="第二个BTN" />
  11. <Button
  12. android:id="@+id/mybutton"
  13. android:layout_width="fill_parent"
  14. android:layout_height="wrap_content"
  15. android:text=" Second Button " />
  16. </LinearLayout>

3、主文件中使用include,将上面两个文件包含进去(activity_main.xml);

[html] view plain copy 
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical" >
  6. <include
  7. android:id="@+id/main1"
  8. layout="@layout/sublayout1" />
  9. <include
  10. android:id="@+id/main2"
  11. layout="@layout/sublayout2" />
  12. <Button
  13. android:id="@+id/another"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:text=" Another Button " />
  17. </LinearLayout>
这样就实现了相同的效果,这里可以看到,include并没有其它的功能,只是把一个XML布局引入进来当做自己的布局,跟直接把引用的这段代码写在include处的效果是一样的。

xml--include用法--xml文件模块化相关推荐

  1. python:xml模块用法-xml处理、修改、删除

    xmltest.xml内容如下: <data> <country name="Liechten"> <rank updated="yes&q ...

  2. decorators.xml的用法 (转)

    from :https://www.cnblogs.com/zhangxiaopeng/p/5442776.html decorators.xml的用法 (转) spring,hibernate框架做 ...

  3. OpenCV学习笔记(四):XML,YAML(.txt,.doc)文件读写操作

    OpenCV学习笔记(四):XML,YAML(.txt,.doc)文件读写操作 一.Write_XML_and_YAML_File(写入XML) #include <opencv2/opencv ...

  4. IDEA 打包项目时xml,yml,properties等文件不复制

    表象:jar 包运行时提示配置找不到. 分析: 配置找不到的原因可能原因: 1.配置文件中的配置项名称和代码${key}中的key值有可能不一致,导致配置项无法正确的匹配 2.配置文件丢失 3.有可能 ...

  5. [QT操作XML]QT读写XML文件,QT修改XML文件

    [QT操作XML]QT读写XML文件 XML简介 QT操作XML,写入.读取.修改 XML效果演示 XML简介 概念:Extensible Markup Language 可扩展标记语言(可扩展:标签 ...

  6. tinyxml2 数组_7.数据本地化CCString,CCArray,CCDictionary,tinyxml2,写入UserDefault.xml文件,操作xml,解析xml...

     数据本地化 A CCUserDefault 系统会在默认路径cocos2d-x-2.2.3\projects\Hello\proj.win32\Debug.win32下生成一个名为UserDef ...

  7. android 颜色资源文件,android中colors.xml颜色设置资源文件的方法

    1. 打开res目录下的values文件夹,双击打开colors.xml文件进行编辑 上代码 #008577 #00574B #D81B60 #ff0000 #0000ff 2. 在res目录下的la ...

  8. 创建带有关联的 XML 架构的 XML 文件 从 XML 文件创建 XML 架构

    一.创建带有关联的XML 架构的XML 文件 1.创建新的 Windows 应用程序项目 首先需要在 Visual Basic 或 Visual C# 中创建新的 Windows 应用程序.创建一个新 ...

  9. 使用xslt将.xml,转换成一个html网页时中文显示乱码怎么办,用dom将数据写到xml中 设置xml文件gbk编码时中文会出现乱码...

    问题:我用dom将数据写到xml中 设置xml文件的编码格式是gbk时再dos下运行jar包中文会出现乱码 如下: 代码如下: StringWriter strWtr = new StringWrit ...

  10. Sandcastle是微软提供的一个根据XML注释和DLL文件生成帮助文件的工具

    2019独角兽企业重金招聘Python工程师标准>>> Sandcastle是微软提供的一个根据XML注释和DLL文件生成帮助文件的工具,目前是在CodePlex上的一个开源项目,可 ...

最新文章

  1. 基于RFID的防伪系统设计
  2. 使用Mootools动态添加Css样式表代码,兼容各浏览器
  3. 网站模糊测试爆破工具Wfuzz
  4. 使用pt-online-schema-change 修复主从数据表数据不一致
  5. sap.ui.core.ComponentContainer
  6. intel服务器最新主板芯片组,intel主板芯片组的介绍
  7. 边缘化搭建 DotNet Core 2.1 自动化发布和部署(下)
  8. 【C++ Priemr | 15】虚函数表剖析(二)
  9. [转载] python类运算符的重载
  10. mysql 5.6.32 linux_centos7 下手动安装MySQL-5.6.32-1.linux_glibc2.5.x86_64.rpm-bundle
  11. Eclipse 插件使用
  12. Tomcat如果默认8080被占用修改端口号和查询端口号地址
  13. 推荐!手把手教你使用Git(转)
  14. acm康复训练记4-world final 2017
  15. 浪曦_Struts2应用开发系列_第2讲.Struts2的类型转换--出现的问题笔记
  16. suitecrm上传附件限制修改
  17. 如何面对大容量的数据存储
  18. RAdam和LookAhead合二为一
  19. 老徐WEB:js入门学习 - 认识javascript
  20. win10计算机休眠设置在哪里,win10休眠选项在哪里设置?如何设置?

热门文章

  1. SEO工作时都会用哪些辅助工具
  2. 专业工业无线串口服务器,MWP-3010R工业无线串口服务器
  3. Pc软件如何测试性能,电脑如何测试性能
  4. Best-first search
  5. 【应用分享】实用工具箱v5.9
  6. ffmpeg 提取字幕
  7. oracle更新行,Oracle更新多行
  8. 主板BIOS报警信号含义一览
  9. 00002 贪婪洞窟.001
  10. Windows Server 2012R2 域与活动目录