Android:在xml中创建一个可变模块

标签 android xml

是否可以创建一个 xml 模块/结构,我可以使用自定义设置将其包含在另一个 xml 中?

想法是制作一个带有搜索栏、文本和文本输入的模块。我将在另一个 xml 中大约需要这个模块 20 次,但它的值(如文本和 ID)对于每个实例都是不同的。

有没有办法在 xml 中做到这一点?

或者有没有办法创建一个使用这个基本 xml 模块的类,并让我通过 xml 设置所需的值?

最佳答案

是的,您可以在 xml 布局中使用合并和包含标签。例如在 grid_layout_view.xml 中你有这样的代码:

<?xml version="1.0" encoding="utf-8"?><GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" 
android:orientation="vertical">
    <include android:id="@+id/my_inside_grid_layout" 
             layout="@layout/grid_layout_inside"/>    </GridLayout>

请注意使用标签来导入包含以下内容的 grid_layout_inside.xml 文件的内容:

    <?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ListView
        android:background="#FFFFFFFF"
        android:layout_gravity="fill">
    </ListView>
    <LinearLayout
        android:layout_gravity="fill_horizontal"
        android:orientation="horizontal"
        android:padding="5dp">

        <Button
            android:text="Cancel"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
        <Button
            android:text="OK"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
     </LinearLayout>

</merge>

希望这有帮助, 鲁本

关于Android:在xml中创建一个可变模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12774740/

相关文章:

android - 如何知道 sdcard 有足够的可用空间写入 Android?

在横向和纵向上具有不同项目的 Android 布局

Android - 在电话上接收联系人更改

android - 无法解析 Widget.MaterialComponents.TextInputLayout.*.ExposedDropdownMenu

xml - 读取/写入包含特殊字符的 XML 节点

iphone - 将 pkcs12 插入到 mobileconfig 文件

xml - 通过 R 中的 Xpath 从 xml 文件中的节点获取值

android - 应用程序处于后台和终止时点击通知有什么区别

xml - 解析xml并按属性获取

xml - 包括具有相同 XML 命名空间的 XSD?