android 中的 include 标签和 merge 标签有什么区别
What is the difference between include tag and merge tag in android
我只需要有人告诉我我是否正确理解何时使用 <include>
以及何时使用 <merge>
。
所以,我制作了一个 header 布局,我想将其包含到其他一些 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>
我的理解正确吗?
我只需要有人告诉我我是否正确理解何时使用 <include>
以及何时使用 <merge>
。
所以,我制作了一个 header 布局,我想将其包含到其他一些 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>
我的理解正确吗?