android布局优化-merge

前言:merge主要是进行UI布局的优化的,删除多余的层级,优化UI。<merge/>多用于替换frameLayout或者当一个布局包含另外一个布局的时候,<merge/>标签用于消除师徒层次结构中多余的视图组。例如你的朱布局文件是垂直的,此时若是你引入一个垂直布局的<include>.这时若是include布局使用的LinearLayout就没意义了,使用的话反而减慢你的UI表现。这时能够使用<merge/>标签优化。<merge>标签也就是排除一个布局插入另外一个布局产生的多余的viewgroup.android

  1.用法布局

复制代码

1 <merge xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent" >
 5 
 6     <TextView
 7         android:layout_width="match_parent"
 8         android:layout_height="wrap_content"
 9         android:text="我是button3" />
10 
11     <Button
12         android:layout_width="match_parent"
13         android:layout_height="wrap_content"
14         android:text="我是button2" />
15 
16 </merge>

复制代码

  布局的效果<这里注意是frameLayout的效果>,为何用在这里呢,由于android有一个默认的FrameLayout的布局优化

 

  2.当把有<merge>标签的布局放在<include>中的时候,就会忽视<merge>xml

复制代码

1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:id="@+id/container"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:orientation="vertical" >
 7 
 8     <include layout="@layout/fragment_main" />
 9 
10 </LinearLayout>

复制代码

  效果blog

  3.<merge>标签的限制io

小白: <merge />标签有什么限制没?fragment

小黑: <merge />只能做为XML布局的根标签使用。当Inflate以<merge />开头的布局文件时,必须指定一个父ViewGroup,而且必须设定attachToRoot为true。im

相关文章
相关标签/搜索