The <merge /> tag helps eliminate redundant view groups in your view hierarchy when including one layout within another.
大意是,merge标签是用来帮助在视图树中减少重复布局的,当一个layout包含另外一个layout时。目的:减少布局层数
效果:减少视图树中的节点个数,加快视图的绘制,提高UI性能;
何时使用:
- 子视图不需要指定任何针对父视图的布局属性
- 假如需要在LinearLayout里面嵌入一个布局(或者视图),而恰恰这个布局(或者视图)的根节点也是LinearLayout,这样就多了一层没有用的嵌套,无疑这样只会拖慢程序速度。而这个时候如果我们使用merge根标签就可以避免那样的问题
子视图不需要指定任何针对父视图的布局属性:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="merge标签使用" />
</RelativeLayout>
放到一个activity中展示,这个是一个很简单的界面就是在界面上展示一个textview
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
我们可以看到这里是这样一个关系 Framelayout-Relativelayout-Textview
但是我们根本没有用到任何Relativelayout的特性,如果改为Linearlayout也是同样道理
我们并没有用到他们的特性,so我们可以把Framelayout,Relativelayout合并
这个时候merge就可以使用了
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="merge标签使用" />
</merge>
与之前相比 Framelayout-Relativelayout-Textview 变为了
Framelayout-Textview 少了一层Relativelayout
第二种情况 消除LinearLayout嵌套
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="activity.szgroup.wy.myactivity.MainActivity"> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" android:clickable="true"/> <include android:id="@+id/keji" layout="@layout/pic_include" /> </LinearLayout>
pic_include.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorAccent"
>
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="@string/jsx"/>
<ImageView
android:id="@+id/pic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/jinsixiong"
/>
</LinearLayout>
- 我们可以看到 activity_main 中用到了 pic_include
并且我们的activity_main和pic_include中都是是linearlayout垂直布局
而且是LinearLayout嵌套LinearLayout这时候我们就可以用merge消除一层布局
我们将pic_include.xml修改为
<?xml version="1.0" encoding="utf-8"?>
<merge
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="activity.szgroup.wy.myactivity.MainActivity">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:clickable="true"/>
<include
android:id="@+id/keji"
layout="@layout/pic_include"
/>
</merge>
也就是用一个Linearlayout实现了原本嵌套的页面
注意事项
- merge必须放在布局文件的根节点上。
- merge并不是一个ViewGroup,也不是一个View,它相当于声明了一些视图,等待被添加。
- merge标签被添加到A容器下,那么merge下的所有视图将被添加到A容器下。
- 因为merge标签并不是View,所以在通过LayoutInflate.inflate方法渲染的时候, 第二个参数必须指定一个父容器,且第三个参数必须为true,也就是必须为merge下的视图指定一个父亲节点。
- 如果Activity的布局文件根节点是FrameLayout,可以替换为merge标签,这样,执行setContentView之后,会减少一层FrameLayout节点。
- 自定义View的时候,根节点如果需要设置成LinearLayout,建议让自定义的View点建议设置成merge,然后自定义的View
- 因为merge不是View,所以对merge标签设置的所有属性都是无效的。
-
在AS中无法预览怎么办?使用
parentTag
指定被装在的parent的布局容器类型,例如tools:parentTag="android.widget.FrameLayout"
,那么就可以预览到当前布局被装在进FrameLayout时候的效果;
merge不是view
就比如说刚才说到的linearlayout 我们消除第二个linearlayout的时候,如果我们本身linearlayout有设置背景,我们只是将linearlayout修改为merge那么是不会有背景效果的