xml--include用法--xml文件模块化
Android在xml文件中可使用include包含其他定义好的布局, 可以将多处用到的布局单独出来,然后用include包含进来,这种包含方法相当于把原来布局的一部分代码独立出来,供大家共同使用,也就相当于面向对向中的类的概念差不多。下面我们逐步讲解include的作用。
先看下我们要实现的整体界面:
一、未使用Include时
通常情况下,我们直接就能写出布局代码,下面是所使用的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" >
- <!-- 第一部分 -->
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:background="#ff0000"
- android:text="第一个BTN" />
- <Button
- android:id="@+id/mybutton"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text=" One Button " />
- <!-- 第二部分 -->
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:background="#00ff00"
- android:text="第二个BTN" />
- <Button
- android:id="@+id/mybutton"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text=" Second Button " />
- <!-- 最后的按钮 -->
- <Button
- android:id="@+id/another"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text=" Another Button " />
- </LinearLayout>
这段代码理解起来一点难度没有,就是几个TextView和几个Button,下面我们用include把这段代码给分割成几个文件,并完成相同的效果;
二、使用Include时
1、先将上面代码标记有“第一部分”的,代码段分离成一个文件(sublayout1.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="wrap_content"
- android:background="#505050"
- android:orientation="vertical" >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:background="#ff0000"
- android:text="第一个BTN" />
- <Button
- android:id="@+id/mybutton"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text=" One Button " />
- </LinearLayout>
2、再将标记有“第二部分”的代码段,分离成第二个文件(sublayout2.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="wrap_content"
- android:orientation="vertical" >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:background="#00ff00"
- android:text="第二个BTN" />
- <Button
- android:id="@+id/mybutton"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text=" Second Button " />
- </LinearLayout>
3、主文件中使用include,将上面两个文件包含进去(activity_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:id="@+id/main1"
- layout="@layout/sublayout1" />
- <include
- android:id="@+id/main2"
- layout="@layout/sublayout2" />
- <Button
- android:id="@+id/another"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text=" Another Button " />
- </LinearLayout>
xml--include用法--xml文件模块化相关推荐
- python:xml模块用法-xml处理、修改、删除
xmltest.xml内容如下: <data> <country name="Liechten"> <rank updated="yes&q ...
- decorators.xml的用法 (转)
from :https://www.cnblogs.com/zhangxiaopeng/p/5442776.html decorators.xml的用法 (转) spring,hibernate框架做 ...
- OpenCV学习笔记(四):XML,YAML(.txt,.doc)文件读写操作
OpenCV学习笔记(四):XML,YAML(.txt,.doc)文件读写操作 一.Write_XML_and_YAML_File(写入XML) #include <opencv2/opencv ...
- IDEA 打包项目时xml,yml,properties等文件不复制
表象:jar 包运行时提示配置找不到. 分析: 配置找不到的原因可能原因: 1.配置文件中的配置项名称和代码${key}中的key值有可能不一致,导致配置项无法正确的匹配 2.配置文件丢失 3.有可能 ...
- [QT操作XML]QT读写XML文件,QT修改XML文件
[QT操作XML]QT读写XML文件 XML简介 QT操作XML,写入.读取.修改 XML效果演示 XML简介 概念:Extensible Markup Language 可扩展标记语言(可扩展:标签 ...
- tinyxml2 数组_7.数据本地化CCString,CCArray,CCDictionary,tinyxml2,写入UserDefault.xml文件,操作xml,解析xml...
数据本地化 A CCUserDefault 系统会在默认路径cocos2d-x-2.2.3\projects\Hello\proj.win32\Debug.win32下生成一个名为UserDef ...
- android 颜色资源文件,android中colors.xml颜色设置资源文件的方法
1. 打开res目录下的values文件夹,双击打开colors.xml文件进行编辑 上代码 #008577 #00574B #D81B60 #ff0000 #0000ff 2. 在res目录下的la ...
- 创建带有关联的 XML 架构的 XML 文件 从 XML 文件创建 XML 架构
一.创建带有关联的XML 架构的XML 文件 1.创建新的 Windows 应用程序项目 首先需要在 Visual Basic 或 Visual C# 中创建新的 Windows 应用程序.创建一个新 ...
- 使用xslt将.xml,转换成一个html网页时中文显示乱码怎么办,用dom将数据写到xml中 设置xml文件gbk编码时中文会出现乱码...
问题:我用dom将数据写到xml中 设置xml文件的编码格式是gbk时再dos下运行jar包中文会出现乱码 如下: 代码如下: StringWriter strWtr = new StringWrit ...
- Sandcastle是微软提供的一个根据XML注释和DLL文件生成帮助文件的工具
2019独角兽企业重金招聘Python工程师标准>>> Sandcastle是微软提供的一个根据XML注释和DLL文件生成帮助文件的工具,目前是在CodePlex上的一个开源项目,可 ...
最新文章
- 基于RFID的防伪系统设计
- 使用Mootools动态添加Css样式表代码,兼容各浏览器
- 网站模糊测试爆破工具Wfuzz
- 使用pt-online-schema-change 修复主从数据表数据不一致
- sap.ui.core.ComponentContainer
- intel服务器最新主板芯片组,intel主板芯片组的介绍
- 边缘化搭建 DotNet Core 2.1 自动化发布和部署(下)
- 【C++ Priemr | 15】虚函数表剖析(二)
- [转载] python类运算符的重载
- mysql 5.6.32 linux_centos7 下手动安装MySQL-5.6.32-1.linux_glibc2.5.x86_64.rpm-bundle
- Eclipse 插件使用
- Tomcat如果默认8080被占用修改端口号和查询端口号地址
- 推荐!手把手教你使用Git(转)
- acm康复训练记4-world final 2017
- 浪曦_Struts2应用开发系列_第2讲.Struts2的类型转换--出现的问题笔记
- suitecrm上传附件限制修改
- 如何面对大容量的数据存储
- RAdam和LookAhead合二为一
- 老徐WEB:js入门学习 - 认识javascript
- win10计算机休眠设置在哪里,win10休眠选项在哪里设置?如何设置?