修改包含的布局上的嵌套属性

chu*_*ubs 9 android attributes view parameterized include

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

<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>
Run Code Online (Sandbox Code Playgroud)

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

<LinearLayout ...>
   <include android:id="@+id/searchHeader" layout="@layout/shared_header" title="Search"/>
   ...
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

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

拥有这样的东西会使创建可重用的视图变得更加简单,但我似乎无法找到任何证据表明merge + include是否可以做到这一点.

chu*_*ubs 5

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