布局优化 合并布局
1、布局重用<include />

<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/common_title" />
</LinearLayout>
2、减少视图层级<merge />
一般情况下,FrameLayout ≈ merge 条件如上
"<merge/>标签在UI的结构优化中起着非常重要的作用,它可以删减多余的层级,优化UI。
<merge/>多用于替换FrameLayout或者当一个布局包含另一个时,<merge/>标签消除视图层次结构中多余的视图组。
例如你的主布局文件是垂直布局,引入了一个垂直布局的include,这是如果include布局使用的LinearLayout就没意义了,使用的话反而减慢你的UI表现。
这时可以使用<merge/>标签优化。"<引用别人的>
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_gravity="center"
android:text="请稍候"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</merge>
3、惰性 不占用空间<ViewStub />
<ViewStub />标签最大的优点是当你需要时才会加载,使用他并不会影响UI初始化时的性能。
各种不常用的布局想进度条、显示错误消息等可以使用<ViewStub />标签,以减少内存使用量,加快渲染速度。
<ViewStub />是一个不可见的,大小为0的View。
当调用inflate()函数的时候,ViewStub被引用的资源替代,并且返回引用的view。
这样程序可以直接得到引用的view而不用再次调用函数findViewById()来查找了。
注:ViewStub目前有个缺陷就是还不支持 <merge /> 标签。
<ViewStub
android:id="@+id/viewstub"
android:layout="@layout/common_progress"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
viewStub.inflate();
![clip_image002[7] clip_image002[7]](http://images2015.cnblogs.com/blog/811126/201510/811126-20151007190356221-1940843893.jpg)



浙公网安备 33010602011771号