include 和merge 的使用对于我们最大的好处就是性能优化,大概可以优化10ms~20ms
1: include 包含,可以直接加载一个xml的layout 布局,非常方便而且节省性能,
可以加载普通的xml 也可以加载merge的xml
需要注意的是include 里面的属性,只能是layout_开头的和id
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<include layout="@layout/includedlayout"/>
<include layout="@layout/merge"/>
</LinearLayout>
2: merge 最大的好处就是和include 结合起来,相对于include 普通的xml 来说,merge 可以减少一层布局,不要少看这一层布局,这一层布局可能就是10ms的加载的时间。
<?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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="include Megered xml" />
</merge>
普通的xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="include normal xml" />
</android.support.constraint.ConstraintLayout>
通过Systrace 来查看:
1: 没有使用include 时候加载的时间
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<!--<include layout="@layout/includedlayout"/>-->
<!--<include layout="@layout/merge"/>-->
<android.support.constraint.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="include normal xml" />
</android.support.constraint.ConstraintLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="include Megered xml" />
</LinearLayout>
打开ddms,采集Systrace点击
在Systrace 的html 里面,我们搜索 inflate
点击m (mark current selection),点击f 可以focus 到这个selection
我们可以发现cpu 使用时间为100ms
2: 我们使用include 去优化,这个时候我们更新布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<include layout="@layout/includedlayout"/>
<include layout="@layout/merge"/>
<!--<android.support.constraint.ConstraintLayout-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content">-->
<!--<TextView-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:text="include normal xml" />-->
<!--</android.support.constraint.ConstraintLayout>-->
<!--<TextView-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:text="include Megered xml" />-->
</LinearLayout>
同样抓去systrace
得到的cpu执行时间是70ms,节省了30ms
2: Merge的层次check,大家可以自己使用ddms来查看