是否可以创建一个 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/