我只需要有人告诉我是否正确理解何时使用<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>
我理解对了吗?
检查包含和合并之间的差异