9

我最近一直在学习合并和包含,我有一个问题我似乎也无法找到答案。假设我有一个布局,它定义了我想添加到多个布局的标题组件。但是,我想更改每个包含使用的每个标题的标题或图标。例如说我有以下布局:

<RelativeLayout android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                style="@style/menu_header">

    <Button android:id="@+id/backButton"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/button"
            android:text="@string/back"/>

    <TextView style="@style/headerTitle"
              android:layout_centerInParent="true"
              android:text="${title}"
              android:layout_height="wrap_content"
              android:layout_width="wrap_content"/>
</RelativeLayout>

然后我可以使用以下方法将其包含在其他布局中:

<LinearLayout ...>
   <include android:id="@+id/searchHeader" layout="@layout/shared_header" title="Search"/>
   ...
</LinearLayout>

我知道我可以修改根元素的任何 layout_* 属性,但我是否可以定义其他替换到布局中的属性,比如在这个例子中说“标题”,而不必创建我自己的 View 子类,添加 declare-styleable价值/资源等中的定义?

拥有这样的东西会使创建可重用视图变得如此简单,但我似乎找不到任何证据表明 merge + include 是否可以做到。

4

1 回答 1

5

答案是否定的。不幸的是,Android 并没有那么强大。您必须创建自己的 ViewGroup 扩展并编写更多代码。

于 2011-07-26T01:12:17.993 回答