Android的<merge>的目的是什么?XML 布局中的标记? [英] What is the purpose of Android's <merge> tag in XML layouts?
问题描述
我在 Romain Guy 的帖子上阅读了<merge/>
标签,但我还是不明白它有什么用处.它是 <Frame/>
标签的替代品,还是像这样使用:
I've read Romain Guy's post on the <merge />
tag, but I still don't understand how it's useful. Is it a sort-of replacement of the <Frame />
tag, or is it used like so:
<merge xmlns:android="....">
<LinearLayout ...>
.
.
.
</LinearLayout>
</merge>
然后
另一个文件中的代码?
then <include />
the code in another file?
推荐答案
<merge/>
很有用,因为它可以摆脱不需要的 ViewGroups,即简单地用于包装其他观点并且本身没有任何意义.
<merge/>
is useful because it can get rid of unneeded ViewGroups, i.e. layouts that are simply used to wrap other views and serve no purpose themselves.
例如,如果您要
另一个文件中的布局而不使用合并,则这两个文件可能如下所示:
For example, if you were to <include/>
a layout from another file without using merge, the two files might look something like this:
layout1.xml:
layout1.xml:
<FrameLayout>
<include layout="@layout/layout2"/>
</FrameLayout>
layout2.xml:
layout2.xml:
<FrameLayout>
<TextView />
<TextView />
</FrameLayout>
在功能上等同于这个单一布局:
which is functionally equivalent to this single layout:
<FrameLayout>
<FrameLayout>
<TextView />
<TextView />
</FrameLayout>
</FrameLayout>
layout2.xml 中的 FrameLayout 可能没有用.
有助于摆脱它.下面是使用合并的样子(layout1.xml 不会改变):
That FrameLayout in layout2.xml may not be useful. <merge/>
helps get rid of it. Here's what it looks like using merge (layout1.xml doesn't change):
layout2.xml:
layout2.xml:
<merge>
<TextView />
<TextView />
</merge>
这在功能上等同于这个布局:
This is functionally equivalent to this layout:
<FrameLayout>
<TextView />
<TextView />
</FrameLayout>
但是由于您使用的是 <include/>
,您可以在其他地方重用布局.它不必仅用于替换 FrameLayouts - 您可以使用它来替换任何对视图外观/行为方式没有帮助的布局.
but since you are using <include/>
you can reuse the layout elsewhere. It doesn't have to be used to replace only FrameLayouts - you can use it to replace any layout that isn't adding something useful to the way your view looks/behaves.
这篇关于Android的<merge>的目的是什么?XML 布局中的标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!