merge标签的使用

今天分享个merge标签,这个标签被定义为优化android布局的

android的绘制是调用 measure  Layout  draw  这三个方法的 ,并且这三个方法是由父类遍历子类调用的,所以如果布局嵌套太深的话,很影响绘制的效率,影响时间。所以在特定情况允许的时候,我们可以引入merge标签。

  • 案列一

集成android源码布局的自定义ViewGroup

以我项目中的为列:

/**
 * @description 空白页面
 * @date: 2018/12/10
 * @author: MR.su
 */
public class LoadingTip extends FrameLayout {

上面的LoadingTip是需要继承FrameLayout的,至于为什么非要继承FrameLayout ,而不是继承ViewGroup,这是因为继承安卓原有的控件,是不需要我们去重写很多方法,很方便

布局控件

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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">

    <RelativeLayout
        android:id="@+id/rely_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center">

        <ImageView
            android:id="@+id/iv_loading"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:src="@drawable/empty"
            tools:ignore="ContentDescription" />

        <TextView
            android:id="@+id/tv_tips"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/iv_loading"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="@dimen/m15"
            android:textColor="@color/c_9d9fa9"
            android:textSize="@dimen/sp14" />

        <TextView
            android:id="@+id/tv_reload"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/tv_tips"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="@dimen/m20"
            android:background="@drawable/tv_rounded_corners"
            android:paddingLeft="15dp"
            android:paddingTop="6dp"
            android:paddingRight="15dp"
            android:paddingBottom="6dp"
            android:text="@string/s_reload"
            android:textColor="@color/c_666"
            android:textSize="@dimen/sp12" />
    </RelativeLayout>

    <ProgressBar
        android:id="@+id/progress_loading"
        android:layout_width="@dimen/m20"
        android:layout_height="@dimen/m20"
        android:layout_gravity="center"
        android:background="@android:color/transparent"
        android:indeterminateBehavior="repeat"
        android:indeterminateDrawable="@drawable/fang_ios_juhua"
        android:visibility="gone" />

可以看出上面的FrameLayout 布局和LoadingTip继承的布局重复了,相当于重复了一个,我们看下安卓绘制的渲染

可以在全局搜索上面的工具,记得这个时候应用要运行在手机上,点击进去,选择所要检查的进程。然后选择需要查看的Activity

发现页面多了一层

现在我们用merge 标签

发现少了一个嵌套.

  • 方案二

用在include标签中,原理是一样的,这个就不测试了

总结:

merger标签相当于这是起了一个包裹的作用,你的全部属性取决于他的父类,所以merge中设置属性是不起作用的想宽高和背景色,这需要注意。

猜你喜欢

转载自blog.csdn.net/xueyoubangbang/article/details/90208024