編輯:關於 Android 程式設計
在 Android 開發中,我們最常用的布局方式莫過於 LinearLayout、RelativeLayout、FrameLayout 等,借助它們可以搭建出各式各樣的界面。然而,如何正確且高效地組織 UI 控件,才是構建優秀 App 的關鍵前提之一。本文將圍繞布局優化展開,介紹日常開發中值得注意的幾條原則,並演示 SDK 自帶的性能檢測工具 HierarchyViewer 的用法。
遵循一些約定俗成的布局原則,能讓我們做出加載高效、復用性強的 UI。核心原則包括:
由於 Android 碎片化嚴重,屏幕尺寸琳瑯滿目,RelativeLayout 通過指定控件間的相對位置,能讓布局在不同屏幕上保持一致表現,適配性更強。當然並非所有場景都適用,需結合具體情況靈活搭配。
實際開發中經常遇到共用 UI 組件,比如帶返回按鈕的頂部導航欄。如果為每個 xml 都寫一遍,不僅重複勞動量大,一旦需要變更還得逐個修改。<include /> 標簽正是為了解決這一痛點:把共用布局單獨抽成一個 xml,再按需導入即可。
新建一個 common_navitationbar.xml 作為導航欄共用布局:
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:padding="10dip">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="Back"
android:textColor="@android:color/black" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Title"
android:textColor="@android:color/black" />
</RelativeLayout>
然後在需要用到導航欄的布局中導入它:
<include
android:layout_alignParentTop="true"
layout="@layout/common_navitationbar" />
這樣既提升了 UI 的制作與復用效率,也讓布局更加規整易維護。
接着進入 SDK 目錄下的 tools 文件夾,找到並運行 HierarchyViewer(運行時保持模擬器或真機上的 App 處於運行狀態),雙擊我們要分析的 App 進程。
隨後便進入 HierarchyViewer 界面,可以清晰看到當前 UI 的布局層次結構及其相互關係。
放大分析圖可以發現,被 include 進來的 common_navitationbar.xml 根節點是一個 RelativeLayout,而包含它的 main.xml 根節點也是 RelativeLayout,其上方還有 FrameLayout 等節點。FrameLayout 是 Activity 布局默認的父容器,再往上是包含整個屏幕(含狀態欄)的 LinearLayout,它還有一個子節點 ViewStub,後文會詳細介紹。
<merge /> 的作用是合並 UI 布局,从而降低嵌套層次。它主要有兩個使用場景:
第一,當 xml 的根布局本就是 FrameLayout 時,可以用 merge 作為根節點。因為 Activity 的內容布局默認已經有一個 FrameLayout 作為父節點,此時再用 FrameLayout 就顯得多餘,用 merge 合並可減少一層嵌套。將 main.xml 根節點改為 merge:
<merge xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/darker_gray">
<include layout="@layout/common_navitationbar" />
</merge>
重新運行並用 HierarchyViewer 查看,會發現之前多出的一個 RelativeLayout 消失了,導航欄內容被直接合並到了 main.xml 中。
第二,當用 include 導入共用布局,且父布局與子布局根節點為同一類型時,可用 merge 把子布局內容合並進父布局,減少一級嵌套。比如新建 common_navi_right.xml 作為導航欄右側按鈕布局,導入後運行會發現它作為子節點嵌套在了導航欄之下。
此時將 common_navi_right.xml 的根節點改為 merge:
<merge xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Ok"
android:textColor="@android:color/black" />
</merge>
再次運行並查看,之前嵌套的那一層 RelativeLayout 便不見了,嵌套層次成功降低。
不少同學對 ViewStub 比較陌生。官方文檔的解釋是:ViewStub 是一個不可見、尺寸為零的 View,用於在運行時按需(懶加載)加載布局資源。當對 ViewStub 調用 inflate() 或設置其可見時,系統才會加載它引入的布局並填充到父容器中。也就是說,在此之前它不占據任何布局空間和系統資源。
它非常適合加載並顯示一些不常用的 View,例如網絡異常提示信息。先新建一個提示布局 common_msg.xml:
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@android:color/white"
android:padding="10dip"
android:text="Message"
android:textColor="@android:color/black" />
</RelativeLayout>
再在 main.xml 中通過 ViewStub 引入:
<ViewStub
android:id="@+id/msg_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout="@layout/common_msg" />
在代碼中控制其顯示與隱藏:
private void showMsgView() {
if (msgView != null) {
msgView.setVisibility(View.VISIBLE);
return;
}
ViewStub stub = (ViewStub) findViewById(R.id.msg_layout);
msgView = stub.inflate(); // 首次調用才真正加載布局
}
private void closeMsgView() {
if (msgView != null) {
msgView.setVisibility(View.GONE);
}
}
由於 ViewStub 在被 inflate 之前幾乎不消耗資源,用它來承載諸如「網絡異常」「空數據」等偶爾才展示的提示,能有效節省內存與初始化時間。
本文介紹了 Android 布局優化的一些實踐,以及 HierarchyViewer 的使用。再次重申幾條核心原則:
掌握了這幾點,就能顯著降低布局層次、提升渲染性能,讓 App 的界面既流暢又易於維護。
Android 屏幕適配實戰:多分辨率下的布局與圖片方案
對剛接觸 Android 的開發者來說,功能做完之後最大的挑戰往往不是邏輯,而是適配——同一份布局,在不同分辨率的機型上顯示效果千差萬別。 先學會查官方文檔 其實 An
插件化開發:360 DroidPlugin 源碼分析(五)Service 預注册占坑
在理解了系統 Activity、Service、BroadcastReceiver 的啟動流程之後,本篇將聚焦一個關鍵問題:360 DroidPlugin 是如何實現 S
Android 性能優化與內存洩漏分析完全指南
性能問題一般可歸納為三類: 1. UI 卡頓與穩定性,這類問題用戶可直接感知,最為重要。 2. 內存問題,主要表現為內存洩漏。若存在洩漏,應用會不斷消耗內存,容易導致頻繁
Android 入門實作:打造一個文件夾管理器(二)檔案操作與批次處理
上一篇我們已經把文件夾清單呈現出來了。這一篇要為清單子項加上事件與操作能力:點擊檔案就開啟,點擊文件夾就進入並遍歷,以此類推直到抵達最後一層。 FileOpreatio