跨多个片段重用一组按钮

时间:2014-10-31 07:04:34

标签: android android-layout android-fragments

我有一个由以下xml文件定义的片段:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_example"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_example"/>

</RelativeLayout>

现在我想在另一个片段中重用ImageButton(以及我将添加的其他按钮),理想情况下

  • 没有复制其定义
  • 将定义保留在xml中,而不是以编程方式添加

即。目标是将同一组按钮叠加在不同的片段中。

有没有办法在单独的xml文件中定义所有按钮并以编程方式在片段创建时加载它们?

1 个答案:

答案 0 :(得分:1)

是的,你可以。

定义不同xml中的所有按钮,例如 layout_buttons.xml

并使用

在每个片段布局中添加它们
<include layout="@layout/layout_buttons" />