1.Merge标签
主要是进行UI布局优化。<merge/>主要是:
1.用来替换FrameLayout或者当一个布局(根节点布局)嵌套布局的时。
2.<merge/>标签用于消除师徒层次结构中多余的视图组。
复制代码
2.实例来表示
(1)没有使用Merge标签代码:
复制代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="demo.zhiyunqiao.com.merge.MainActivity"
tools:layout_editor_absoluteY="81dp">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"/>
</RelativeLayout>
复制代码
用DDMS检测出来的布局层级:
复制代码
<?xml version="1.0" encoding="utf-8"?>
<merge
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="demo.zhiyunqiao.com.merge.MainActivity"
tools:layout_editor_absoluteY="81dp">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"/>
</merge>
复制代码
DDMS检测结果:
3.需要注意:
1.某些时候,自己定义可重用的布局包括了过多的层级标签,比方我们比如:这种话。使用<include>包括上面的布局的时候。系统会自己主动忽略merge层级
实例表示:
(1)include代码:
复制代码
4.merge的使用限制
merge只能作为XML布局的根标签使用。当Inflate以merge开头的布局文件时,必须指定一个父ViewGroup,并且必须设定attachToRoot为true。
复制代码