android---xml---include用法(导入另一个XML文件到本布局中)
2015-08-21 12:04
696 查看
一未使用Include时
二使用Include时
先将上面代码标记有第一部分的代码段分离成一个文件sublayout1xml
再将标记有第二部分的代码段分离成第二个文件sublayout2xml
主文件中使用include将上面两个文件包含进去activity_mainxml
Android在xml文件中可使用include包含其他定义好的布局, 可以将多处用到的布局单独出来,然后用include包含进来,这种包含方法相当于把原来布局的一部分代码独立出来,供大家共同使用,也就相当于面向对向中的类的概念差不多。下面我们逐步讲解include的作用。
先看下我们要实现的整体界面:
[html]view plaincopyprint?
<?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把这段代码给分割成几个文件,并完成相同的效果;
<?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>
<?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>
<?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>
这样就实现了相同的效果,这里可以看到,include并没有其它的功能,只是把一个XML布局引入进来当做自己的布局,跟直接把引用的这段代码写在include处的效果是一样的。
虽然内容比较简单,可能有些朋友还是想实地运行一下源码,下面给出源码下载地址,不要分,仅供分享。
示例源码下载:http://download.csdn.net/detail/harvic880925/6697495
请大家尊重作者版权,转载请标明出处:http://blog.csdn.net/harvic880925/article/details/17263275
二使用Include时
先将上面代码标记有第一部分的代码段分离成一个文件sublayout1xml
再将标记有第二部分的代码段分离成第二个文件sublayout2xml
主文件中使用include将上面两个文件包含进去activity_mainxml
Android在xml文件中可使用include包含其他定义好的布局, 可以将多处用到的布局单独出来,然后用include包含进来,这种包含方法相当于把原来布局的一部分代码独立出来,供大家共同使用,也就相当于面向对向中的类的概念差不多。下面我们逐步讲解include的作用。
先看下我们要实现的整体界面:
一、未使用Include时
通常情况下,我们直接就能写出布局代码,下面是所使用的XML代码:[html]view plaincopyprint?
<?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>
<?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);
[html]view plaincopyprint?<?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>
<?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):
[html]view plaincopyprint?<?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>
<?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);
[html]view plaincopyprint?<?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 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>
这样就实现了相同的效果,这里可以看到,include并没有其它的功能,只是把一个XML布局引入进来当做自己的布局,跟直接把引用的这段代码写在include处的效果是一样的。
虽然内容比较简单,可能有些朋友还是想实地运行一下源码,下面给出源码下载地址,不要分,仅供分享。
示例源码下载:http://download.csdn.net/detail/harvic880925/6697495
请大家尊重作者版权,转载请标明出处:http://blog.csdn.net/harvic880925/article/details/17263275
相关文章推荐
- Android事件传递机制
- Android设置透明效果
- Android 实现个性的ViewPager切换动画 实战PageTransformer(兼容Android3.0以下)
- Android推送服务——百度云推送
- Android中自定义checkbox样式
- android 优秀的几个框架
- android 如何设置图片颜色的透明度
- Android静态图片人脸识别的完整demo(附完整源码)
- Android编程中manifest中<uses-sdk>
- Android的setTag() 函数含义
- 提升Android ListView性能的几个技巧
- 关于android.view.WindowLeaked异常的解决方案
- Android中Parcelable接口用法
- Android:sqlite3:not found
- Intent在Activity中的应用
- android制作类似支付圆圈和打钩界面ProgressWheel
- JNI学习2——jni入门篇
- androoid makefile 学习
- 欢迎使用CSDN-markdown编辑器
- Android 自定义控件实现刮刮卡效果 真的就只是刮刮卡么