当前位置:操作系统 > 安卓/Android >>

Android--使用include调用布局

Android在xml文件中可使用include包含其他定义好的布局,
可以将多处用到的布局单独出来,然后用include包含进来
 
 
下面是一个简单的示例,两个xml文件:
 
main.xml文件:
[html]  
<span style="font-size:14px;"><?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/sublayout" />  
  
    <include  
        android:id="@+id/main2"  
        layout="@layout/sublayout" />  
  
    <Button  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text=" Start Another Activity " />  
  
</LinearLayout></span>  
 
 
 
 
sublayout.xml文件:
 
[html]  
<span style="font-size:14px;"><?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:text="SubLayout" />  
  
    <Button  
        android:id="@+id/mybutton"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:text=" A Button " />  
  
</LinearLayout></span>  
 
 
补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,