25

如何将 xml 数据包含到其他 xml 数据中?

我的应用程序有一个标头 xml 文件,我想在我的特殊其他内容 xml 中使用它。有没有办法将标题包含到我的内容中?

4

3 回答 3

36

使用包含标签

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"     
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
 >
  <!-- Header -->
  <include
    android:id="@+id/container_header_lyt"  
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:layout_above=...
    android:layout_toLeftOf=...
    layout="@layout/header_logo_lyt" //Name of the xml layout file you want to include
    />     

...

</RelativeLayout>
于 2011-06-10T13:12:14.600 回答
12

您必须使用 <merge> 标签声明您的“body”xml 布局,才能在主布局上使用 <include> 标签。

<?xml version="1.0" encoding="utf-8"?>
<merge>
    <ImageView android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:scaleType="center"
        android:src="@drawable/image" / >

    <TextView android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_gravity="center_horizontal|bottom"
        android:background="#AA000000"
        android:textColor="#ffffffff"
        android:text="Some Text" / >
</merge>

这是您的 <include > 标签的内容

于 2011-06-10T14:03:12.730 回答
6

你应该使用 <include> 标签:Re-using Layouts with

于 2011-06-10T13:11:10.863 回答