san*_*one 40 merge android include android-layout
我只需要有人告诉我,我是否正确理解何时使用<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>
Run Code Online (Sandbox Code Playgroud)
我以这种方式将它包含在其他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>
Run Code Online (Sandbox Code Playgroud)
这将很好,没有问题.但是为了优化代码,我必须<merge>在包含的布局中使用.所以不top layout应该有标签,<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>
Run Code Online (Sandbox Code Playgroud)
我理解正确吗?
oni*_*nit 28
根据我的理解,它会将merge元素设置为视图层次结构中的较高元素.包含将简单地将整个视图组放在那里.因此,使用您的示例,视图层次结构应如下所示:
合并:
LinearLayout (root)
|
TextView
Run Code Online (Sandbox Code Playgroud)
包括:
LinearLayout (root)
|
LinearLayout
|
TextView
Run Code Online (Sandbox Code Playgroud)
因此,您将在视图层次结构中有一个不需要的额外LinearLayout.但是,有时您需要该中间视图.在你的情况下,你不会,因为LinearLayouts都有相同的布局参数,没有其他差异.
xyz*_*xyz 27
是的,你理解正确.merge用作伪父元素以减少视图树中的级别数.只需查看此链接,就可以给出很好的解释merge.
在您的头文件中
<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>
Run Code Online (Sandbox Code Playgroud)
<LinearLayout>当您的文件包含在您提到的其他文件中时,没有任何区别.所以使用它是一件好事merge.
因为在XML中你必须使用单个父元素,其余的XML元素应该包含在其中,你应该使用merge单个父元素,并且可以避免添加不必要的父布局.
如果要以不同的方式应用布局,请避免"合并",而不是在文件中定义布局.
| 归档时间: |
|
| 查看次数: |
11862 次 |
| 最近记录: |