Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android 版面優化實戰:FrameLayout 與 merge、include、ViewStub

編輯:Android 開發實例

FrameLayout:最單純的畫布

官方對 FrameLayout 的描述是「最簡單的版面物件」:它像一塊留白的區域,你把單一內容填進去即可,例如一張要顯示的圖片。所有子元素都從左上角開始配置,無法 individually 指定位置;後加入的子元素會直接疊在前一個上面,將其部分或全部遮住(除非後者是透明的)。

實務上可以把 FrameLayout 當成 canvas,再搭配 android:layout_gravity 決定每個子元素靠在哪個方位:

<FrameLayout
    xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ImageView
        android:id="@+id/image"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="center"
        android:src="@drawable/candle" />

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textColor="#00ff00"
        android:text="hello" />

    <Button
        android:id="@+id/start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:text="Start" />
</FrameLayout>

圖片鋪滿全螢幕、文字置中、按鈕貼底,這正是 FrameLayout 最典型的用法:層疊式覆蓋。

用 Hierarchy Viewer 檢查版面層級

在 SDK 的 tools 目錄底下有個 hierarchyviewer.bat。先啟動模擬器與要分析的程式,再執行這個批次檔,選擇模擬器與目標程式,按下「Load View Hierarchy」就會開始解析,結果也能另存為 PNG。它最直接的價值,是讓你看見自己的版面究竟嵌套了幾層。

<merge>:拿掉多餘的容器

從上圖往往會發現畫面上明明只有一個 FrameLayout,實際卻存在兩個(外層是系統加上去的)。既然 layout 檔需要一個根容器才算合法,直接刪掉 FrameLayout 會導致檔案無效,這時候就該改用 <merge>:

<merge xmlns:android="https://schemas.android.com/apk/res/android">
    <ImageView
        android:id="@+id/image"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="center"
        android:src="@drawable/candle" />

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textColor="#00ff00"
        android:text="hello" />

    <Button
        android:id="@+id/start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:text="Start" />
</merge>

使用限制有兩點:一是 <merge> 只能作為 XML layout 檔的根元素;二是用程式碼 inflate 時,必須呼叫 View inflate(int resource, ViewGroup root, boolean attachToRoot) 並指定一個 ViewGroup 當容器,且把 attachToRoot 設為 true。

<include>:重用版面片段

若多處需要同一段版面設計,用 include 直接搬過來即可:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <include android:id="@+id/layout1" layout="@layout/relative" />
    <include android:id="@+id/layout2" layout="@layout/relative" />
    <include android:id="@+id/layout3" layout="@layout/relative" />

</LinearLayout>

要留意的是 @layout/relative 指向的是 res/layout/relative.xml 這個檔案,而不是某個 view 的 id。另外 include 除了可以覆寫 id,也能覆寫 android:layout_width、android:layout_height 等其他屬性,讓同一段版面在不同場合呈現不同尺寸。

<ViewStub>:延遲載入

ViewStub 是一個不可見、大小為 0 的輕量 View,最適合用來做「用到才載入」的延遲載入,概念等同 Java 常見的 lazy loading。

當你呼叫 setVisibility() 將它設為可見,或直接呼叫 inflate() 時,它所引用的資源才會被初始化,並取代 ViewStub 本身的位置填入父容器。在這之前,ViewStub 雖然存在於檢視樹中,但因為極為輕量,對效能的影響微乎其微。

<ViewStub android:id="@+id/stub"
    android:inflatedId="@+id/subTree"
    android:layout="@layout/mySubTree"
    android:layout_width="120dip"
    android:layout_height="40dip" />

上面這段的意思是:先用 id「stub」找到它,初始化 mySubTree 之後,stub 會從父容器中移除,mySubTree 佔據原本的位置,並可用 inflatedId 指定的「subTree」來引用;同時它被填進一個 120dip × 40dip 的區域。官方推薦的初始化寫法如下:

ViewStub stub = (ViewStub) findViewById(R.id.stub);
View inflated = stub.inflate();

inflate() 會回傳被載入的 View,省去再呼叫一次 findViewById 的麻煩。需要注意的是,ViewStub 目前還不支援 <merge /> 標籤。

layoutopt:官方提供的版面健檢工具

tools 目錄裡還有個 layoutopt.bat,可以分析 layout 並給出優化建議。

用法:layoutopt <xml 檔案或目錄清單>
參數:一個或多個 layout XML 檔(以空白分隔),或包含這些檔案的目錄路徑。

layoutopt G:\StudyAndroid\UIDemo\res\layout\main.xml
layoutopt G:\StudyAndroid\UIDemo\res\layout\main.xml G:\StudyAndroid\UIDemo\res\layout\relative.xml
layoutopt G:\StudyAndroid\UIDemo\res\layout

整理一下順序

版面優化的思路大致是:先用 Hierarchy Viewer 看見問題(層數太深、多餘容器),再用 <merge> 壓平層級、用 <include> 消除重複、用 <ViewStub> 把非必要的重量級元件延後載入,最後用 layoutopt 做一遍自動健檢。層數每少一層,measure 與 layout 的開銷跟著下降,在低端機與長清單場景尤其有感。

本文整理自「學習Android」部落格:https://android.blog.51cto.com/268543/308090

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved