如何从另一个自定义XML布局引用自定义XML布局?

时间:2012-03-31 21:42:16

标签: android layout xml-layout

给定XML layout(称之为“内部”布局),如何从另一个自定义XML布局(称为“外部”布局)引用内部布局?这可能仅使用XML,还是唯一的程序化解决方案?

内部布局:

<?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="wrap_content"
  android:padding="6dip"
  >
  <ImageView
    android:id="@+id/productImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_marginLeft="6dip"
    />
  <TextView
    android:id="@+id/productName"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@id/productImage"
    android:textAppearance="?android:attr/textAppearanceLarge"
    />
</RelativeLayout>

外部布局:

<?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="wrap_content"
  >
  <!-- Embed inner XML layout here -->
  <Button
    android:id="@+id/productButtonAddToShoppingList"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="<!-- refer to inner layout -->"
    android:layout_marginTop="2dip"
    android:text="add to shopping list"
    />
</RelativeLayout>

1 个答案:

答案 0 :(得分:3)

基本上你这样使用the <include /> tag

<include layout="@layout/inner_layout" />

另请参阅:http://developer.android.com/resources/articles/layout-tricks-reuse.html

相关问题