简说
安卓在加载视图的时候,不管根节点是线性布局仍是相对布局,系统都会在上一层添加一个<FrameLayout.>,也就是说任何形式的布局都会被包含在framelayout布局中html
减小视图层级<merge />
<merge/>标签在UI的结构优化中起着很是重要的做用,它能够删减多余的层级,优化UI。<merge/>多用于替换FrameLayout或者当一个布局包含另外一个时,<merge/>标签消除视图层次结构中多余的视图组。例如你的主布局文件是垂直布局,引入了一个垂直布局的include,这是若是include布局使用的LinearLayout就没意义了,使用的话反而减慢你的UI表现。这时能够使用<merge/>标签优化。android
<pre name="code" class="html"><pre name="code" class="html"><?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:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <include layout="@layout/progress_overlay"></include> </LinearLayout>
progress_overlay.xml文件中
<?xml version="1.0" encoding="utf-8"?> <merge xmlns:android="http://schemas.android.com/apk/res/android"> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="工a"/> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="fdshfdhfhs"/> </merge>

如今,当你添加该布局文件时(使用<include />标签),系统忽略<merge />节点而且直接添加两个Button。更多<merge />
布局
本文同步分享在 博客“早起的年轻人”(CSDN)。
若有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一块儿分享。优化