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

标签 android view attributes include parameterized

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

<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/

相关文章:

Android EditText 文本的偏移量?

attributes - C# 中的 lambda 表达式和属性

sql - 对可以具有不同属性的实体建模的最佳方法是什么?

android - 打开/解压缩 zip 文件时 Cordova 构建 Gradle 错误

android - android.text.format.DateFormat.format 方法的正确使用方法是什么

java - android studio 的设计预览中布局为空

asp.net-mvc - 序列不包含元素错误 - MVC(模型和数据访问)

android - 在 Android Studio 中自动在多台设备上运行应用

java - 以编程方式膨胀 View

javascript - 未捕获的类型错误 : Cannot read property 'player' of undefined