這個概念很常見,使用基本版面配置結構會形成最有效率的版面配置。不過,您在應用程式中新增的每個小工具和版面配置,都需要初始化、版面配置和繪圖。舉例來說,使用 LinearLayout
的巢狀執行個體會導致檢視區塊階層過高。此外,為使用 layout_weight
參數的 LinearLayout
建立巢狀結構的多個例項可能會特別高昂,因為每個子項都需要測量兩次。如果重複加載版面配置 (例如在 RecyclerView
中使用時),這個做法尤其重要。
本文說明如何使用版面配置檢查器和 Lint 檢查版面配置,並進行最佳化處理。
檢查版面配置
Android SDK 工具提供版面配置檢查器工具,可讓您在應用程式執行期間分析版面配置。使用這項工具可協助您發現版面配置效能的效率不彰。
版面配置檢查器可讓您在已連結的裝置或模擬器中選取執行中的程序,然後顯示版面配置樹狀結構。每個區塊上的交通號代表其 Measure、Layout 和 Draw 效能,可協助您找出潛在問題。
例如,圖 1 顯示在 RecyclerView
中做為項目的版面配置。這個版面配置左側顯示小型點陣圖圖片,右側是兩個堆疊的文字項目。特別重要的是,這類版面配置會多次加載,因為效能提升會使效能提升而最佳化。
版面配置檢查器會顯示可用裝置及其執行元件的清單。在「Windows」分頁中選擇所需元件,然後按一下「Layout Inspector」,即可查看所選元件的版面配置階層。例如,圖 2 顯示清單項目的版面配置,如圖 1 所示。
修改版面配置
由於上述版面配置效能因巢狀 LinearLayout
而會變慢,您可以分割版面配置 (也就是使版面配置縮到淺和寬,而非縮小和深度),藉此提升效能。ConstraintLayout
做為根節點允許這類版面配置。將這個設計轉換為使用 ConstraintLayout
時,版面配置會成為雙層階層:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/root" android:layout_width="match_parent" android:layout_height="52dp" android:background="#e4e6e4" android:padding="4dp"> <ImageView android:id="@+id/image" android:layout_width="48dp" android:layout_height="48dp" android:background="#5c5c74" android:contentDescription="An example box" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/title" android:layout_width="0dp" android:layout_height="0dp" android:layout_marginStart="4dp" android:background="#745c74" app:layout_constraintBottom_toTopOf="@+id/subtitle" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@id/image" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/subtitle" android:layout_width="0dp" android:layout_height="0dp" android:background="#7e8d6e" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="@id/title" app:layout_constraintTop_toBottomOf="@+id/title" /> </androidx.constraintlayout.widget.ConstraintLayout>
新版面配置的檢查看起來會像這樣:
這個版面配置會乘以清單裡的每個項目,因此可乘以這個版面配置的優點。
大部分的差異在於 LinearLayout
設計中使用 layout_weight
,這可能會減慢測量速度。這是每種版面配置適當用途的範例。請審慎考慮是否需要使用版面配置粗細。
在部分複雜的版面配置中,系統可能會浪費重複測量相同的 UI 元素一次。這種現象就是所謂的「雙重課」。如要進一步瞭解重複作業和避免方式,請參閱「效能與檢視區塊階層」。
使用 Lint
建議您對版面配置檔案執行 Lint 工具,搜尋可能的檢視區塊階層最佳化。Lint 會取代 layoutopt 工具,並提供更多功能。以下是 Lint 規則的範例:
-
使用複合可繪項目。您可以更有效率地處理包含
ImageView
和TextView
的LinearLayout
,將其視為複合可繪項目。 -
合併根框架。如果版面配置的根層級是不提供背景或邊框間距的
FrameLayout
,您可以將其替換為合併標記,這樣會更有效率。 - 移除無用的葉子。移除沒有子項或沒有背景的版面配置,因為這類版面配置是隱藏的,讓版面配置階層更簡潔且更有效率。
-
移除不方便的父項。您可以移除沒有同層級、非
ScrollView
或根版面配置,且沒有背景的子項版面配置。您也可以將子項檢視畫面直接移至父項,使版面配置階層更精簡,也更有效率。 -
避免使用深層版面配置。含有過多巢狀結構的版面配置會對效能造成負面影響。建議您使用扁平的版面配置 (例如
ConstraintLayout
) 來改善效能。Lint 檢查的預設深度上限是 10。
Lint 工具的另一個優點是將其整合至 Android Studio。每當您編譯程式時,Lint 就會自動執行。透過 Android Studio,您也可以針對特定建構變數或所有建構變數執行 Lint 檢查。
您也可以透過「File」>「Settings」>「Project Settings」選項,在 Android Studio 中管理檢查設定檔並設定檢查。系統隨即顯示「檢查設定」頁面以及支援的檢查:
Lint 會自動修正部分問題、提供建議給其他使用者,並直接前往違規的程式碼進行審查。