1. 程式人生 > >android佈局優化之標籤的使用

android佈局優化之標籤的使用

學習android知識,卻經常忘記,是應該把知識總結為文字. -2017年6月21日07:51:19

1,<merge>標籤的作用

<merge />標籤用於減少View樹的層次來優化Android的佈局.

通過一個例子來理解這句話:

建立mergelayout.xml,作為Activity的佈局:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="match_parent"
             android:layout_height="match_parent">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@mipmap/ic_launcher"
        android:scaleType="fitXY"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="GreenRobot"
        android:layout_marginBottom="20dp"
        android:textColor="#000"
        android:background="#22000000"
        android:padding="12dp"
        android:layout_gravity="center_horizontal|bottom"/>
</FrameLayout>
執行一下效果圖:


使用sdk/tools/裡的工具monitor.bat,點選Dump View Hierarchy for UI Automator按鈕,檢視這部分的檢視結構:


可以看到xml中的FrameLayout節點上面還有一個FrameLayout節點,這個xml中的FrameLayout節點其實是個無用的父節點(Useless parent),白白增加了佈局的層級.

那麼怎麼辦呢?這就是<merge>標籤派上用場的時候了.當LayoutInflater遇見<merge>標籤時,就會跳過<merge>標籤並且把<merge>標籤的子view新增到<merge>標籤的父view中.修改mergelayout.xml佈局,替換根節點<FrameLayout>為<merge>,如下:

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@mipmap/ic_launcher"
        android:scaleType="fitXY"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="GreenRobot"
        android:layout_marginBottom="20dp"
        android:textColor="#000"
        android:background="#22000000"
        android:padding="12dp"
        android:layout_gravity="center_horizontal|bottom"/>
</merge>
檢視檢視結構:


可以發現那個useless parent不見了,這就是達到了<merge />標籤用於減少View樹的層次來優化Android的佈局的效果.

2,<merge>標籤什麼時候用

2.1,當你發現了useless parent時,使用<merge>標籤

例子見1中的例子.

2.2,<merge>標籤和<include>標籤配合使用,彌補<include>標籤的不足

2.3,當建立一個自定義組合控制元件時,使用<merge>標籤

例子如下: 最終的效果圖是:

底部的兩個按鈕是一個自定義組合控制元件OkCancelBar,程式碼如下

public class OkCancelBar extends LinearLayout {
    public OkCancelBar(Context context) {
        this(context, null);
    }

    public OkCancelBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        setOrientation(HORIZONTAL);
        setGravity(Gravity.CENTER);
        setWeightSum(1.0f);

        LayoutInflater.from(context).inflate(R.layout.okcancel_bar, this, true);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.OkCancelBar, 0, 0);
        // 獲取xml中設定的屬性值
        String okLabel = typedArray.getString(R.styleable.OkCancelBar_okLabel);
        String cancelLabel = typedArray.getString(R.styleable.OkCancelBar_cancelLabel);
        // 獲取控制元件
        Button btnOk = (Button) findViewById(R.id.okcancelbar_ok);
        Button btnCancel = (Button) findViewById(R.id.okcancelbar_cancel);
        // 給控制元件設定值
        btnOk.setText(okLabel == null ? "Ok" : okLabel);
        btnCancel.setText(cancelLabel == null ? "Cancel" : cancelLabel);
        typedArray.recycle();
    }
}
對應的layout/okcancel_bar.xml為
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">

    <include
        android:id="@+id/okcancelbar_ok"
        layout="@layout/okcancelbar_button"/>

    <include
        android:id="@+id/okcancelbar_cancel"
        layout="@layout/okcancelbar_button"/>
</merge>

對應的layout/okcancelbar_button.xml為
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              xmlns:tools="http://schemas.android.com/tools"
              android:textSize="18sp"
              tools:text="AAAAA">

</Button>
自定義控制元件的attrs.xml檔案為
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="OkCancelBar">
        <attr name="okLabel" format="string"/>
        <attr name="cancelLabel" format="string"/>
    </declare-styleable>
</resources>

作為Activity佈局的mergelayout.xml為
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:okCancelBar="http://schemas.android.com/apk/res-auto">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY"
        android:src="@mipmap/ic_launcher" />
    <com.android.demo.OkCancelBar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:paddingTop="8dp"
        android:gravity="center_horizontal"
        android:background="#AA000000"
        okCancelBar:okLabel="Save"
        okCancelBar:cancelLabel="Don't save" >

    </com.android.demo.OkCancelBar>
</merge>

程式碼貼完了,分析一下:

利用monitor.bat工具,切換到Hierachy View下,檢視Tree View,截圖如下(只截取了與分析相關的內容):


如果把okcancelbar.xml根節點改為LinearLayout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <include
        android:id="@+id/okcancelbar_ok"
        layout="@layout/okcancelbar_button"/>

    <include
        android:id="@+id/okcancelbar_cancel"
        layout="@layout/okcancelbar_button"/>
</LinearLayout>
再看一下Tree View檢視,可以看到多出了LinearLayout節點,增加了檢視層級:



3,使用<merge>標籤需要注意的地方

3.1,merge標籤只能作為複用佈局的root元素來使用.

3.2,當Inflate以<merge />開頭的佈局檔案時,必須指定一個父ViewGroup,並且必須設定attachToRoot為true。

參考資料:

1,http://www.tuicool.com/articles/jyyUV33

2,http://blog.csdn.net/xyz_lmn/article/details/14524567

相關推薦

android佈局優化標籤的使用

學習android知識,卻經常忘記,是應該把知識總結為文字. -2017年6月21日07:51:19 1,<merge>標籤的作用 <merge />標籤用於減少View樹的層次來優化Android的佈局. 通過一個例子來理解這句話: 建立merg

Android佈局優化merge標籤詳解

我們都知道View的繪製流程需要經歷measure、layout、draw這個三個過程,如果佈局巢狀層次比較深的話,每一步都需要進行遍歷所有子View進行對應的measure、layout、draw過程,由此就會降低繪製效率,巢狀越多,耗時就越多;其實不光光只會影響view的繪製效率,同

Android佈局優化使用style提取重複使用屬性

如何優化佈局程式碼?使之看起來既簡潔又得到優化呢? 在專案中寫佈局的時候常常會遇到很多空間使用到了很多相同的屬性,例如一個TextView使用到了marginLeft和marginRight,其他很

Android效能優化佈局優化

          佈局優化可以通過減少佈局層級來提高,儘量減少使用效能低的佈局,LineaLayout的效率最高,在可以使用LinearLayout或者RelativeLayout時,選擇LinearLayout。因為RelativeLayout測量較為複雜,需要測量水平和

Android 佈局優化 include+merge+ViewStub標籤詳解

include 、merge、ViewStub標籤詳解 一.include標籤 include標籤常用於將佈局中的公共部分提取出來供其他layout使用,以實現佈局模組化。 程式碼 1.1.公共Layout <?xml versio

Android進階——佈局優化靈活藉助ViewStub實現懶載入

引言 相信在開發Android App的過程中,我們會常常遇到這樣的業務需求,需要在執行時根據資料動態決定顯示或隱藏某個View和佈局。通常就是把可能用到的View先寫在佈局裡,再初始化其可見性都設為View.GONE,然後在程式碼中根據資料動態的更改它的可見

Android效能優化:XML佈局檔案優化

Android中XML佈局檔案的使用非常頻繁,在載入XML佈局的時候,如果對XML檔案其進行優化,將會提高載入的效率。 HierarchyViewer工具 再開始介紹之前先說一下HierarchyViewer工具的使用。 不合理的佈局會使我們的應用程式

Android佈局演示底部標籤

在很多Android的程式的佈局中需要在底部放上一個導航用的標籤欄,但是Android本身並沒有像iPhone SDK那樣提供相關的佈局物件來實現這個功能,不過沒關係,我們可以來自己實現這個佈局, 佈局實現思路: 用一個LinearLayout來裝下所有的標籤按鈕,同時設定

android佈局優化的三大標籤

1、佈局重用 <include /> 標籤能夠重用佈局檔案,簡單的使用如下: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

Android UI優化merge標籤的使用

 前面已經介紹了<include />標籤的使用,有需要的可以檢視前面文章。 本文主要介紹Android UI優化之<merge />與<include />標籤的混合使用: 使用<merge />標籤,減少多餘的層級,優

Android App優化ANR詳解

nic orm rim manage dal private rom syn UNC 引言 背景:Android App優化, 要怎麽做? Android App優化之性能分析工具 Android App優化之提升你的App啟動速度之理論基礎 Android App優化

Android——效能優化SparseArray

相信大家都用過HashMap用來存放鍵值對,最近在專案中使用HashMap的時候發現,有時候 IDE 會提示我這裡的HashMap可以用SparseArray或者SparseIntArray等等來代替。 SparseArray(稀疏陣列).它是Android內部特有的api,標準的jdk是沒有這

Android效能優化較精確的獲取影象顯示到螢幕上的時間

轉載自:http://blog.desmondyao.com/android-show-time/ 這兩天我的包工頭歪龍木·靈魂架構師·王半仙·Yrom給我派了一個活:統計App冷啟動時間。這個任務看上去不難,但是要求統計出來的時間要準,要特別準。 意思就是,我必須要按Activity繪製到

Android應用優化冷啟動優化

前言 事件發生在發包上線的前兩天,在某某雲進行移動測試時,提示冷啟動速度低於平均值的問題,之前自己也曾嘗試過優化,但是發現效果並不是很明顯,作為一個有追求的開發者,趁著有點空閒時間,要好好研究一下冷啟動優化問題。 App的啟動流程 我們可以瞭解一下官方文件《App startup time》對App啟動

Android開發優化的強引用、軟引用、弱引用的使用

本文轉載至:http://www.jianshu.com/p/8488079a939b 引言 早在JDK1.2,Java就把物件的引用分為四種級別,從而使程式能更加靈活的控制物件的生命週期。這四種級別由高到低依次為:強引用、軟引用、弱引用和虛引用。 但是平時我們的程式碼中似乎很

android電量優化Battery Historian工具使用

前幾天寫了關於androidAPP效能優化總結的文章,還沒有看的話可以看一下,這文章提到了電量優化,android耗電分析所用到的工具battery-historian,這裡做一個總結. 在 Android5.0 以前,在應用中測試電量消耗比較麻煩,也不準確,5.0 之後專門引入了一個獲取裝置上

Android效能優化圖片壓縮優化

1 分類Android圖片壓縮結合多種壓縮方式,常用的有尺寸壓縮、質量壓縮、取樣率壓縮以及通過JNI呼叫libjpeg庫來進行壓縮。 參考此方法:Android-BitherCompress 備註:對於資源圖片直接使用:tiny壓縮 2 質量壓縮(1)原理:保持畫素的前提下改變圖片的位深及透明度,(即:通

Android佈局屬性講解標籤屬性

RelativeLayout 第一類:屬性值為true可false android:layout_centerHrizontal        水平居中 android:layout_centerVertical     &nb

Android應用優化流暢度

前言 對於現今市面上針對於使用者互動的應用,都有使用列表去展示資訊。列表對於使用者來說是十分好的瀏覽、接收資訊的一個控制元件。對於產品來說,列表流暢度的重要性就不言而喻了。而流暢度的好壞,對一個產品的基本體驗和口碑有著極大的影響。然而Android手機與iPhone手機對比,第一點往

Android應用優化程式碼檢測優化

前言 最近換了新的公司,面對新的程式碼大家都有不同的熟悉過程和方法。在我的角度來說,利用程式碼檢測工具,可以更直接地去熟悉程式碼邏輯和業務邏輯,表現得自己去程式碼質量很有追求,最重要當然是在公司的任務管理工時上面顯得自己積極向上啦。不過在修改程式碼之前,你要根據專案的分工、明確在公司