Android在xml文件中可使用include包含其他定義好的布局,可以將多處用到的布局單獨出來,然後用include包含進來
下面是一個簡單的示例,兩個xml文件:
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/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>
sublayout.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:text="SubLayout" />
<Button
android:id="@+id/mybutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=" A Button " />
</LinearLayout>
更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11