編輯:關於 Android 程式設計
本文將介紹如何讓你的應用自適應各種屏幕尺寸,主要途徑包括:讓布局充分自適應、按屏幕配置加載合適的 UI、確保正確的布局用在正確的設備上,以及提供能隨屏幕伸縮的圖片。
要讓布局自適應不同屏幕,應在設置視圖寬高時使用 wrap_content 和 match_parent。前者會把視圖尺寸設為恰好容納內容的最小值;後者(API 8 之前叫 fill_parent)則讓視圖延伸填滿父布局。用這兩個值替代硬編碼尺寸,視圖要麼只佔用必要空間,要麼撐滿全部可用空間。例如:
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
xmlns:android="https://schemas.android.com/apk/res/android">
<LinearLayout
android:gravity="center"
android:id="@+id/linearLayout1"
android:layout_height="50dp"
android:layout_width="match_parent">
<ImageView
android:id="@+id/imageView1"
android:layout_gravity="left"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:paddingRight="30dp"
android:src="@drawable/logo" />
<View
android:id="@+id/view1"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="wrap_content" />
</LinearLayout>
</LinearLayout>
這樣一來,整個布局就能正確適應不同屏幕,甚至橫豎屏切換也無礙。

靠多層嵌套 LinearLayout 加上 wrap_content、match_parent,已經能搭出相當複雜的布局。但 LinearLayout 無法精確控制子視圖之間的位置關係,子視圖只能一個挨一個地排列。如果你需要更靈活的排布,RelativeLayout 會是更好的選擇——它允許子控件之間以相對定位的方式安排彼此的位置。例如:
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="https://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/label"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="Type here:" />
<EditText
android:id="@+id/entry"
android:layout_below="@id/label"
android:layout_height="wrap_content"
android:layout_width="match_parent" />
<Button
android:id="@+id/ok"
android:layout_alignParentRight="true"
android:layout_below="@id/entry"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:text="OK" />
<Button
android:layout_toLeftOf="@id/ok"
android:layout_alignTop="@id/ok"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Cancel" />
</RelativeLayout>
下圖是它在 QVGA 屏幕上的效果:

換到更大的屏幕上:

可以發現,無論屏幕大小如何變化,視圖之間的相對位置都保持不變。
伸縮式布局雖能適配各種屏幕,但未必能提供最佳體驗。更好的做法是根據屏幕配置加載不同布局,這可以通過配置限定符(configuration qualifiers)實現——它允許程序在運行時按當前設備配置自動加載合適的資源。
很多應用為了支持大屏設備會實現「雙面板」模式:左側列出條目,右側展示內容。平板和電視屏幕夠大,可同時顯示兩個面板;手機屏幕一次只容得下一個。為此你可能需要:
res/layout/main.xml,單面板(默認)布局:
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
xmlns:android="https://schemas.android.com/apk/res/android">
<fragment
android:id="@+id/headlines"
android:layout_height="fill_parent"
android:layout_width="match_parent"
android:name="com.example.android.newsreader.HeadlinesFragment" />
</LinearLayout>
res/layout-large/main.xml,雙面板布局:
<LinearLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="horizontal"
xmlns:android="https://schemas.android.com/apk/res/android">
<fragment
android:id="@+id/headlines"
android:layout_height="fill_parent"
android:layout_marginRight="10dp"
android:layout_width="400dp"
android:name="com.example.android.newsreader.HeadlinesFragment" />
<fragment
android:id="@+id/article"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:name="com.example.android.newsreader.ArticleFragment" />
</LinearLayout>
第二個布局目錄名帶有 large 限定符,被認定為大屏的設備(如 7 寸以上平板)會自動加載它,小屏設備則加載默認布局。
Size 限定符有個惱人之處:large 到底指多大?很多開發者希望能更自由地按屏幕寬度加載布局。這正是 Android 3.2 之後引入 Smallest-width 限定符的原因。它允許你以 dp 為單位指定一個具體的最小寬度。例如 7 寸平板最小寬度約為 600dp,想用 sw600dp 表示「寬度在 600dp 以上時採用雙面板」。
res/layout-sw600dp/main.xml 即為雙面板布局,其內容與上面的 layout-large/main.xml 相同。這意味著最小寬度大於 600dp 的設備會選 layout-sw600dp 版本,更小的則選默認版本。
不過早於 Android 3.2 的設備無法識別 sw600dp,因此仍需同時保留 large 限定符,這就導致 res/layout-large 與 res/layout-sw600dp 下要放兩份相同的 main.xml。下一節我們來看看如何避免這種重複。
由於 Smallest-width 僅在 3.2 及以上有效,兼容舊系統時仍需配合 Size 限定符。若想在手機上顯示單面板、在 7 寸平板及以上顯示多面板,你需要 main.xml(單面板)、layout-large(多面板)、layout-sw600dp(多面板)三處文件,而後兩者內容完全一致。為消除重複,可使用別名技巧:
res/layout/main.xml:單面板布局
res/layout/main_twopanes.xml:雙面板布局
再新增兩個文件。res/values-large/layout.xml:
<resources>
<item name="main" type="layout">@layout/main_twopanes</item>
</resources>
res/values-sw600dp/layout.xml:
<resources>
<item name="main" type="layout">@layout/main_twopanes</item>
</resources>
這兩個文件內容相同,卻並未真正定義布局,只是給 main 起了個別名 main_twopanes,從而避免了重複定義。
有些布局橫豎屏都表現良好,但多數情況下仍可再優化。以 News Reader 示例為例,它在不同屏幕尺寸與方向下的呈現各不相同:小屏豎屏單面板帶 logo、7 寸平板橫屏雙面板帶 action bar、10 寸平板豎屏窄雙面板等等。這些布局都定義在 res/layout/ 下,並借助布局別名讓設備按配置加載正確版本。
例如 res/values-sw600dp-land/layouts.xml:
<resources>
<item name="main_layout" type="layout">@layout/twopanes</item>
<bool name="has_two_panes">true</bool>
</resources>
再配合 res/values-sw600dp-port、res/values-large-land 等目錄下的同名文件,就能讓橫豎屏、不同尺寸各自加載合適的布局,並通過 has_two_panes 這個布爾值在代碼中判斷當前是否為雙面板。
支持不同屏幕通常也意味著圖片資源要能自適應。比如一個按鈕的背景圖必須能隨按鈕大小伸縮。若直接用普通圖片,運行時會被均勻拉伸或壓縮,效果往往很糟。解決方案是使用 nine-patch 圖片——一種特殊處理過的 PNG,你可以指定哪些區域可拉伸、哪些不可。
從一張普通圖片開始:

用 SDK tools 目錄下的 draw9patch 工具打開它,在左、上邊框上繪製標記可拉伸的區域,在右、下邊框上標記內容放置的區域:

注意圖片邊框上的黑色像素:上、左邊框表示拉伸時拉伸黑點標記的位置,下、右邊框表示內容的放置區域。此外,文件後綴必須是 .9.png,系統正是靠這個後綴來辨別 nine-patch 与普通 PNG。使用時(如 android:background="@drawable/button"),系統會依控件大小自動拉伸指定部分:

Android 開發實例:搜尋 SD 卡中的檔案
需求背景 在 Android 應用中,經常需要瀏覽或搜尋外部儲存空間的檔案。當檔案數量龐大時,若能提供關鍵字搜尋,使用者就能更快找到目標。本文以一個簡單範例說明如何建立搜
Android Activity 基礎詳解
活動(Activity)是一種可以包含使用者介面的組件,主要用於與使用者互動。一個應用程式中可以包含零個或多個活動。 1. 活動的基本用法 首先建立一個工程,命名為 Te
打造可播放 GIF 動畫的 GifImageView 自定義控件
Android 原生控件並不支持播放 GIF 圖片。我們知道,若想顯示一張圖片,通常會借助 ImageView,但如果把一張 GIF 設置給 ImageView,它只會顯
Android 常用布局小結:FrameLayout、LinearLayout、RelativeLayout 與漸變背景
1. FrameLayout(幀布局) 效果是多個圖層依次疊加,像畫一幅畫:先放一張背景 ImageView,再往上疊其他元素。 如果想讓三個按鈕分別在水平方向左對齊、居