【发布时间】:2016-04-18 11:28:08
【问题描述】:
我只需要有人告诉我是否正确理解了何时使用<include> 以及何时使用<merge>。
所以,我制作了一个标题布局,我想将它包含在其他 XML 布局中:
这是我添加两个视图的合并示例
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Header text" />
</LinearLayout>
我以这种方式将它包含到其他一些 XML 中(这是非常基本的):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<include android:id="@+id/header" layout="@layout/top"
android:layout_width="fill_parent" android:layout_height="wrap_content"
/>
</LinearLayout>
这会很好用,没有问题。但是为了优化代码,我必须在包含的布局中使用<merge>。所以顶部布局不应该有标签<LinearLayout>,但它必须看起来像这样:
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Header text" />
</merge>
我理解正确吗?
【问题讨论】:
-
是的,你理解正确 100%。
标签: android xml layout merge include