我最近一直在学习合并和包含,但我有一个问题,我似乎也无法找出答案。假设我有一个布局,它定义了一个标题组件,我想将它添加到多个布局中。但是,我想根据每个包含用法更改每个标题的标题或图标。例如说我有以下布局:
<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_* 属性,但是我可以定义其他属性来替换到布局中,比如本例中的“title”,而不必创建我自己的 View 子类,添加在值/资源等中声明样式化定义?
有这样的东西会使创建可重用 View 变得非常简单,但我似乎找不到任何证据表明合并 + 包含是否可以做到这一点。
最佳答案
答案是否定的。不幸的是,Android 并没有那么强大。您必须创建自己的 ViewGroup 扩展并编写更多代码。
关于android - 修改包含的布局上的嵌套属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6669239/