[精]Android merge 和 include 的使用

本文介绍了Android中`include`和`merge`的使用,强调它们在性能优化上的作用,尤其是在减少布局层级和提升加载速度方面的优势。通过Systrace工具的分析,展示了如何使用`merge`与`include`组合可以将CPU执行时间从100ms降低到70ms,优化效果显著。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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来查看

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值