有些人可以澄清< include>的用法和< merge> [英] Can some clarify usage of <include> and <merge>

查看:123
本文介绍了有些人可以澄清< include>的用法和< merge>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只需要有人告诉我,我是否理解了何时使用< include> 以及何时< merge>

I just need someone to tell me if I understood correctly when to use <include> and when <merge>.

所以,我制作了一个标题布局,我希望将其包含在其他XML布局中:

So, I make a header layout which I want to include into some other XML layout:

<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中(这是非常基本的):

And I include it into some other XML this way (which is pretty basic):

<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> 标签,但它必须如下所示:

This will work well, no issue about it. But in order to optimize the code, I have to use <merge> in the layout which gets included. So the top layout should not have a tag <LinearLayout> but it must look like this:

<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>

我是否理解正确?

推荐答案

是的,你理解正确。 merge 用作伪父元素,以减少视图树中的级别数。
只需查看链接,它就会非常好解释合并

在您的标题文件中

Yes you understood it correctly. merge is used as pseudo parent element to reduce the number of levels in view trees. Just check this link, it gives very good explanation of merge.
In your header file

<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>   

< LinearLayot> 不会当您的文件包含在您提到的其他文件中时有任何区别。因此,使用 merge 是一件好事。

因为在XML中你必须使用单个父元素,并且应该包含其余的XML元素在其中,您应该使用 merge 作为单个父元素,并且可以避免添加不必要的父布局。

当您想要应用时,请避免合并布局与布局不同,在文件中定义了您的内容。

<LinearLayot> doesn't make any difference when your file is included in other file you mentioned. So it's a good thing to use merge instead.
Since in XML you must use a single parent element and the rest of the XML elements should be included in it, you should use merge as single parent element and can avoid adding unnecessary parent layout.
Just avoid 'merge' when you want to apply a layout differently than layout is defined in file in which your content is inclded.

这篇关于有些人可以澄清&lt; include&gt;的用法和&lt; merge&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆