道客优

1234
Android性能优化之布局优化
2019-03-02 刀刻油 阅读:970

布局优化主要包括以下三个方面

1、减少布局文件的层级

2、尽量使用简单高效的ViewGroup,比如FrameLayoutLinaerLayout

3、布局复用,通过include和merge实现

include、merge标签案例
在layout文件中创建layout/merge.xml文件内容如下:

    <merge xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:id="@+id/mTV_incloud_merge"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/white"
            android:text="" />
    </merge>

在Activity的layout布局引入:

    <include layout="@layout/merge" />

使用ViewStub

ViewStub是一个轻量级的布局,它有三个优点:

1、它的宽度、高度只有0,不参与绘制过程。
2、按需加载,不占用空间。
3、显示ViewStub中的布局时候,ViewStub会被替换掉,并且会被从布局中移除。

xml代码:

    <ViewStub
        android:id="@+id/viewstub"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:background="@android:color/white"
        android:inflatedId="@id/view"
        android:layout="@layout/viewstub"
        android:padding="2dp" />

值得注意的是ViewStub中layout布局中不能使用merge


  

推荐阅读: