android---xml---include用法(导入另一个XML文件到本布局中)

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

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

一、未使用Include时

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

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  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.     <!-- 第二部分 -->  
  19.     <TextView    
  20.         android:layout_width="fill_parent"    
  21.         android:layout_height="wrap_content"    
  22.         android:background="#00ff00"  
  23.         android:text="第二个BTN" />    
  24.     
  25.     <Button    
  26.         android:id="@+id/mybutton"    
  27.         android:layout_width="fill_parent"    
  28.         android:layout_height="wrap_content"    
  29.         android:text=" Second Button " />   
  30.           
  31.   <!-- 最后的按钮 -->  
  32.   <Button    
  33.         android:id="@+id/another"  
  34.         android:layout_width="wrap_content"    
  35.         android:layout_height="wrap_content"    
  36.         android:text=" Another Button " />    
  37.        
  38. </LinearLayout>  

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

二、使用Include时

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

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  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.     
  8.     <TextView    
  9.         android:layout_width="fill_parent"    
  10.         android:layout_height="wrap_content"    
  11.         android:background="#ff0000"  
  12.         android:text="第一个BTN" />    
  13.     <Button    
  14.         android:id="@+id/mybutton"    
  15.         android:layout_width="fill_parent"    
  16.         android:layout_height="wrap_content"    
  17.         android:text=" One Button " />    
  18.     
  19. </LinearLayout>  

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

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  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.     
  12.     <Button    
  13.         android:id="@+id/mybutton"    
  14.         android:layout_width="fill_parent"    
  15.         android:layout_height="wrap_content"    
  16.         android:text=" Second Button " />    
  17.   
  18. </LinearLayout>  

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

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  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.     <include    
  8.         android:id="@+id/main1"    
  9.         layout="@layout/sublayout1" />    
  10.     
  11.     <include    
  12.         android:id="@+id/main2"    
  13.         layout="@layout/sublayout2" />    
  14.     
  15.     <Button    
  16.         android:id="@+id/another"  
  17.         android:layout_width="wrap_content"    
  18.         android:layout_height="wrap_content"    
  19.         android:text=" Another Button " />    
  20.     
  21. </LinearLayout>  

这样就实现了相同的效果,这里可以看到,include并没有其它的功能,只是把一个XML布局引入进来当做自己的布局,跟直接把引用的这段代码写在include处的效果是一样的。

虽然内容比较简单,可能有些朋友还是想实地运行一下源码,下面给出源码下载地址,不要分,仅供分享。

示例源码下载:http://download.csdn.net/detail/harvic880925/6697495

请大家尊重作者版权,转载请标明出处:http://blog.csdn.net/harvic880925/article/details/17263275

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值