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

用 layoutopt 優化 Android 佈局:找出冗餘與低效的巢狀結構

編輯:Android 開發環境

設計一個好看的 Android 佈局本身就是門手藝。花幾個小時調好適配多種螢幕之後,通常你並不想再回頭改。但問題是,層層嵌套的佈局往往效率極差,會直接拖累渲染效能。

值得慶幸的是,Android SDK 自帶一個名為 layoutopt 的命令行工具,可以自動分析佈局檔、找出多餘的元件,幫助你降低複雜度、減少記憶體消耗。

為什麼要及早優化

優化是需要判斷力的。效能優良的代碼固然重要,但寫出優秀代碼的成本很高,通常我們不會為只執行一次的臨時功能做優化。可一旦應用的反應變遲鈍,或者拖慢了系統裡其他程式,使用者就會用腳投票——下載量與評價都會受影響。

在開發早期就優化佈局,是既省成本又能見效的做法。layoutopt 的價值就在於把「哪裡多餘」這件事自動化,讓你不必靠肉眼在 XML 裡翻找。

第一步:準備工作

layoutopt 需要在命令列中運行。如果你平時習慣图形介面,這裡得稍微花點時間適應。

強烈建議把 Android 工具目錄加入系統環境變量 PATH,這樣直接敲工具名就能運行,不必每次輸入完整路徑。注意 SDK 中有兩個工具目錄:/tools 與 /platform-tools,layoutopt 位於前者,而 adb 位於後者。

運行 layoutopt

用法很簡單,後面跟一個佈局檔或佈局檔所在目錄即可。但有一個容易忽略的細節:必須使用完整路徑,即使你當前就在該目錄中。

D:\d\tools\eclipse\article_ws\Nothing\res\layout>layoutopt D:\d\tools\eclipse\article_ws\Nothing\res\layout\main.xml
D:\d\tools\eclipse\article_ws\Nothing\res\layout\main.xml
D:\d\tools\eclipse\article_ws\Nothing\res\layout>

反之,如果只寫檔名:

D:\d\tools\eclipse\article_ws\Nothing\res\layout>layoutopt main.xml
D:\d\tools\eclipse\article_ws\Nothing\res\layout>

不會有任何輸出。所以如果你「看不到任何建議」,第一個要懷疑的就是路徑沒給對、檔案根本沒被解析。

解讀 layoutopt 的輸出

layoutopt 給出的是建議,你可以自行判斷是否採納。下面看幾類典型輸出。

1. 無用的佈局

設計階段我們經常反覆拖動元件,有些容器最終變成空殼:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:orientation="vertical">
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:text="TextView"
            android:layout_height="wrap_content"></TextView>
    </LinearLayout>
</LinearLayout>

工具會立刻指出內層的 LinearLayout 是多餘的:

11:17 This LinearLayout layout or its LinearLayout parent is useless

輸出每行開頭的兩個數字是建議的行號範圍。

2. 根節點可以替換

有些建議之間會互相矛盾,需要自己取捨:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:orientation="vertical">
        <TextView ... />
        <TextView ... />
    </LinearLayout>
</FrameLayout>

輸出為:

5:22 The root-level <FrameLayout/> can be replaced with <merge/>
10:21 This LinearLayout layout or its FrameLayout parent is useless

第一條建議可行但並非必需——我們需要兩個 TextView 垂直排列,所以 LinearLayout 應該保留。第二條則可以採納,直接刪掉多餘的 FrameLayout。

有意思的是,這個工具並非全知全能。如果我們給 FrameLayout 加上一個背景屬性再跑一次,第一條建議會消失(因為 merge 無法承載背景),但第二條建議依然會出現——它意識到了不能合並背景,卻在檢查 LinearLayout 時「忘記」了父容器提供了 LinearLayout 自身不具備的屬性。這提醒我們:工具的輸出是參考,不是命令。

3. 視圖數量過多

每個 View 都會佔用記憶體。若單個佈局包含過多視圖,layoutopt 會給出類似下面的提示:

-1:-1 This layout has too many views: 83 views, it should have <= 80!
-1:-1 This layout has too many views: 82 views, it should have <= 80!
-1:-1 This layout has too many views: 81 views, it should have <= 80!

門檻是 80。新設備或許撐得住更多,但如果確實出現效能問題,建議認真對待這條提示,考慮用 ListView / RecyclerView 之類的可回收容器替代一次性鋪開的大量子視圖。

4. 巢狀層數過深

佈局不應有過多嵌套。layoutopt(以及 Android 團隊)建議控制在 10 層以內,即便是最大的平板螢幕也不該超過。RelativeLayout 或 ConstraintLayout 是常見的解法,可以用「扁平化」的方式取代多層 LinearLayout 組合;新版工具中的視覺化編輯器讓這件事輕werd不少。

-1:-1 This layout has too many nested layouts: 12 levels, it should have <= 10!
305:318 This LinearLayout layout or its RelativeLayout parent is possibly useless
307:314 This LinearLayout layout or its FrameLayout parent is possibly useless
310:312 This LinearLayout layout or its LinearLayout parent is possibly useless

巢狀警告通常伴隨若干「無用佈局」提示,這其實是好事——它能幫助你精準定位可以摘除的層級,不必整個螢幕重新設計。

總結

layoutopt 是一個快速、易用的佈局分析工具。它只負責發現問題,取捨仍然要靠你自己判斷。

採納建議後,效能不會立刻有戲劇性提升,但沒有理由讓一個不必要的複雜佈局拖累整個應用,更何況它還會顯著抬高後續維護的成本。簡潔的佈局不僅縮短開發週期,也能減少測試與維護的工作量。因此,請在開發早期就把佈局優化納入流程,而不是等使用者回饋「很卡」之後才手忙腳亂。

最後補充一句現況:layoutopt 屬於早期 SDK 工具,在當前版本中已被移除。今天更推薦使用 Android Studio 內建的 Layout Inspector 與 Layout Validation 工具,它們能直接在可視化層面呈現巢狀深度與效能問題,比讀命令行輸出直觀得多。

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