The <merge /> tag helps eliminate redundant view groups in your view hierarchy when including one layout within another. For example, if your main layout is a vertical LinearLayout in which two consecutive views can be re-used in multiple layouts, then the re-usable layout in which you place the two views requires its own root view. However, using another LinearLayout as the root for the re-usable layout would result in a vertical LinearLayout inside a vertical LinearLayout. The nested LinearLayout serves no real purpose other than to slow down your UI performance.
<合并/ >标记有助于消除冗余在视图层次结构视图组其中一个在另一个布局。例如,如果您的主要布局垂直LinearLayout连续两个视图可以重复使用在多个布局,然后你把两个视图的可重复使用的布局需要自己的根视图。然而,使用另一个LinearLayout作为可重用的布局将导致根垂直LinearLayout在一个垂直的LinearLayout。嵌套的LinearLayout以外没有任何作用减缓UI性能。
当LayoutInflater遇到这个标签时,它会跳过它,并将<merge />内的元素添加到<merge />的父元素里。
<merge />标签极其有用。然而它也有以下两个限制:
· <merge />只能作为XML布局的根标签使用
· 当Inflate以<merge />开头的布局文件时,必须指定一个父ViewGroup,并且必须设定attachToRoot为true(参看inflate(int, android.view.ViewGroup, Boolean)方法)。