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中設置屬性是不起作用的想寬高和背景色,這需要注意。

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章