編輯:Android 開發實例
本文將帶你了解如何讓應用支援各種不同螢幕尺寸,主要途徑有以下幾種:
要確保布局能自適應各種螢幕,你應該在視圖的寬高上使用 "wrap_content" 與 "match_parent"。設為 "wrap_content" 時,視圖的寬高會被設成剛好能容納內容的最小值;而設為 "match_parent"(在 Android API 8 之前稱為 "fill_parent")時,視圖的寬高會延伸填滿整個父布局。
用 "wrap_content" 與 "match_parent" 取代寫死尺寸,視圖要嘛只佔用剛好需要的空間,要嘛填滿所有可用空間。例如:
- <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <LinearLayout android:layout_width="match_parent"
- android:id="@+id/linearLayout1"
- android:gravity="center"
- android:layout_height="50dp">
- <ImageView android:id="@+id/imageView1"
- android:layout_height="wrap_content"
- android:layout_width="wrap_content"
- android:src="@drawable/logo"
- android:paddingRight="30dp"
- android:layout_gravity="left"
- android:layout_weight="0" />
- <View android:layout_height="wrap_content"
- android:id="@+id/view1"
- android:layout_width="wrap_content"
- android:layout_weight="1" />
- <Button android:id="@+id/categorybutton"
- android:background="@drawable/button_bg"
- android:layout_height="match_parent"
- android:layout_weight="0"
- android:layout_width="120dp"
- style="@style/CategoryButtonStyle"/>
- </LinearLayout>
- <fragment android:id="@+id/headlines"
- android:layout_height="fill_parent"
- android:name="com.example.android.newsreader.HeadlinesFragment"
- android:layout_width="match_parent" />
- </LinearLayout>
注意上例中如何用 "wrap_content" 與 "match_parent" 定義控制項的寬高,這讓整個布局能正確適應不同螢幕,甚至橫屏。
下圖是這個布局分別在直屏與橫屏下的顯示結果,可以留意控制項的寬高是隨螢幕自適應的。
透過多層嵌套 LinearLayout 並搭配 "wrap_content" 與 "match_parent",已經能搭出相當複雜的布局。但 LinearLayout 無法讓你精確控制子視圖之間的位置關係,它的子視圖只能一個挨著一個排下去。如果你需要更靈活的排列方式,而非單純排成一列或一行,RelativeLayout 會是更好的選擇。它允許子控制項之間以相對定位的方式擺放,例如讓一個子視圖靠螢幕左側對齊、另一個靠右側對齊。
例如:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <TextView
- android:id="@+id/label"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Type here:"/>
- <EditText
- android:id="@+id/entry"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_below="@id/label"/>
- <Button
- android:id="@+id/ok"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_below="@id/entry"
- android:layout_alignParentRight="true"
- android:layout_marginLeft="10dp"
- android:text="OK" />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_toLeftOf="@id/ok"
- android:layout_alignTop="@id/ok"
- android:text="Cancel" />
- </RelativeLayout>
下圖展示這個布局在 QVGA 螢幕上的結果。
下圖展示它在更大螢幕上的結果。
可以注意到,即使螢幕尺寸改變,各視圖之間的相對位置都沒有變動。
雖然上述幾種方式能解決螢幕適配問題,但那些靠伸縮控制項來適應各種螢幕的布局,未必能提供最好的用戶體驗。你的應用不僅要能自適應,還應該能依螢幕配置載入不同布局,這可以透過配置限定符(configuration qualifiers)來實現。配置限定符讓程式在執行時依當前裝置配置自動載入合適的資源(例如為不同尺寸螢幕設計不同布局)。
如今很多應用為了支援大螢幕,都會實作「two pane」模式(左側面板展示含子項的清單,右側面板展示內容)。平板與電視的螢幕夠大,能同時顯示兩個面板;而手機一次只能顯示一個,兩個面板得分開呈現。因此要實作這種布局,你可能需要以下檔案:
res/layout/main.xml,single-pane(預設)布局:
- <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <fragment android:id="@+id/headlines"
- android:layout_height="fill_parent"
- android:name="com.example.android.newsreader.HeadlinesFragment"
- android:layout_width="match_parent" />
- </LinearLayout>
res/layout-large/main.xml,two-pane 布局:
- <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="horizontal">
- <fragment android:id="@+id/headlines"
- android:layout_height="fill_parent"
- android:name="com.example.android.newsreader.HeadlinesFragment"
- android:layout_width="400dp"
- android:layout_marginRight="10dp"/>
- <fragment android:id="@+id/article"
- android:layout_height="fill_parent"
- android:name="com.example.android.newsreader.ArticleFragment"
- android:layout_width="fill_parent" />
- </LinearLayout>
請注意第二個布局的目錄名含有 large 限定符,那些被判定為大螢幕的裝置(例如 7 吋以上的平板)會自動載入此布局,小螢幕裝置則載入另一個預設布局。
使用 Size 限定符有個讓不少開發者頭疼的問題:large 到底是指多大?很多應用希望能更自由地為不同螢幕載入不同布局,不管它們是否被系統判定為 "large"。這正是 Android 為什麼在 3.2 之後引入了 "Smallest-width" 限定符。
Smallest-width 限定符允許你設定一個具體的最小值(以 dp 為單位)來描述螢幕。例如 7 吋平板的最小寬度是 600dp,所以若你想讓 UI 在這種螢幕上顯示 two pane、在更小螢幕上顯示 single pane,就可以用 sw600dp 表示想在 600dp 以上寬度的螢幕使用 two pane 模式。
res/layout/main.xml,single-pane(預設)布局:
- <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <fragment android:id="@+id/headlines"
- android:layout_height="fill_parent"
- android:name="com.example.android.newsreader.HeadlinesFragment"
- android:layout_width="match_parent" />
- </LinearLayout>
res/layout-sw600dp/main.xml,two-pane 布局:
- <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="horizontal">
- <fragment android:id="@+id/headlines"
- android:layout_height="fill_parent"
- android:name="com.example.android.newsreader.HeadlinesFragment"
- android:layout_width="400dp"
- android:layout_marginRight="10dp"/>
- <fragment android:id="@+id/article"
- android:layout_height="fill_parent"
- android:name="com.example.android.newsreader.ArticleFragment"
- android:layout_width="fill_parent" />
- </LinearLayout>
這意味著,最小螢幕寬度大於 600dp 的裝置會選 layout-sw600dp/main.xml(two-pane),更小螢幕的裝置則選 layout/main.xml(single-pane)。
然而,早於 Android 3.2 的系統無法識別 sw600dp 這個限定符,所以你仍需同時使用 large 限定符,也就是要在 res/layout-large 與 res/layout-sw600dp 兩個目錄下各放一份相同的 main.xml。下一節你會看到如何避免這種重複定義。
Smallest-width 限定符僅在 Android 3.2 及之後的系統有效,因此你也需要同時用 Size 限定符(small、normal、large、xlarge)來兼容更早的系統。例如你想在手機上顯示 single-pane、在 7 吋平板及更大螢幕上顯示 multi-pane,就需要提供以下檔案:
後兩個檔案內容完全相同,為了解決這種重複,你可以使用別名技巧。例如定義以下布局:
再加入以下兩個檔案:
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。兩份 layout.xml 都只引用 @layout/main_twopanes,就避免了重複定義布局檔。
有些布局在橫屏與直屏下都顯示得不錯,但多數情況下仍有調整空間。在 News Reader 範例程式中,布局在不同螢幕尺寸與方向下的顯示是這樣的:
這些布局都定義在 res/layout/ 目錄下,為了讓裝置依螢幕配置載入正確布局,程式需要使用布局別名來實現。
res/layout/onepane.xml:
- <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <fragment android:id="@+id/headlines"
- android:layout_height="fill_parent"
- android:name="com.example.android.newsreader.HeadlinesFragment"
- android:layout_width="match_parent" />
- </LinearLayout>
res/layout/onepane_with_bar.xml:
- <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <LinearLayout android:layout_width="match_parent"
- android:id="@+id/linearLayout1"
- android:gravity="center"
- android:layout_height="50dp">
- <ImageView android:id="@+id/imageView1"
- android:layout_height="wrap_content"
- android:layout_width="wrap_content"
- android:src="@drawable/logo"
- android:paddingRight="30dp"
- android:layout_gravity="left"
- android:layout_weight="0" />
- <View android:layout_height="wrap_content"
- android:id="@+id/view1"
- android:layout_width="wrap_content"
- android:layout_weight="1" />
- <Button android:id="@+id/categorybutton"
- android:background="@drawable/button_bg"
- android:layout_height="match_parent"
- android:layout_weight="0"
- android:layout_width="120dp"
- style="@style/CategoryButtonStyle"/>
- </LinearLayout>
- <fragment android:id="@+id/headlines"
- android:layout_height="fill_parent"
- android:name="com.example.android.newsreader.HeadlinesFragment"
- android:layout_width="match_parent" />
- </LinearLayout>
res/layout/twopanes.xml:
- <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="horizontal">
- <fragment android:id="@+id/headlines"
- android:layout_height="fill_parent"
- android:name="com.example.android.newsreader.HeadlinesFragment"
- android:layout_width="400dp"
- android:layout_marginRight="10dp"/>
- <fragment android:id="@+id/article"
- android:layout_height="fill_parent"
- android:name="com.example.android.newsreader.ArticleFragment"
- android:layout_width="fill_parent" />
- </LinearLayout>
res/layout/twopanes_narrow.xml:
- <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="horizontal">
- <fragment android:id="@+id/headlines"
- android:layout_height="fill_parent"
- android:name="com.example.android.newsreader.HeadlinesFragment"
- android:layout_width="200dp"
- android:layout_marginRight="10dp"/>
- <fragment android:id="@+id/article"
- android:layout_height="fill_parent"
- android:name="com.example.android.newsreader.ArticleFragment"
- android:layout_width="fill_parent" />
- </LinearLayout>
現在所有需要的布局都定義好了,剩下只要用限定符讓各裝置依螢幕配置載入正確布局即可。你可以使用布局別名技術:
res/values/layouts.xml:
- <resources>
- <item name="main_layout" type="layout">@layout/onepane_with_bar</item>
- <bool name="has_two_panes">false</bool>
- </resources>
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/layouts.xml:
- <resources>
- <item name="main_layout" type="layout">@layout/onepane</item>
- <bool name="has_two_panes">false</bool>
- </resources>
res/values-large-land/layouts.xml:
- <resources>
- <item name="main_layout" type="layout">@layout/twopanes</item>
- <bool name="has_two_panes">true</bool>
- </resources>
res/values-large-port/layouts.xml:
- <resources>
- <item name="main_layout" type="layout">@layout/twopanes_narrow</item>
- <bool name="has_two_panes">true</bool>
- </resources>
支援不同螢幕尺寸,通常也意味著你的圖片資源需要具備自適應能力。例如一顆按鈕的背景圖,必須能隨按鈕大小的變化而變化。
如果你想用普通圖片來實現,很快就會發現結果令人失望,因為執行時圖片會被均勻地拉伸或壓縮。解決方案是使用 nine-patch 圖片——這是一種經過特殊處理的 PNG 圖片,你能指定哪些區域可拉伸、哪些不可。
因此,當你設計需要在不同大小控制項中使用的圖片時,最好的方式就是用 nine-patch 圖片。要將圖片轉換成 nine-patch,可以從一張普通圖片開始:
然後用 SDK 附帶的 draw9patch 工具(位於 SDK 的 tools 目錄)打開它,在圖片的左邊框與上邊框上繪製,標記哪些區域可被拉伸;也可以在右邊框與下邊框上繪製,標記內容要放置的區域。結果如下圖:
注意圖片邊框上的黑色像素:上邊框與左邊框的部分代表圖片需要拉伸時就拉伸黑點標記的位置;下邊框與右邊框的部分代表內容將被放置的區域。
同時要留意,這張圖片的副檔名是 .9.png。你必須使用這個副檔名,因為系統正是靠它來區分 nine-patch 圖片與普通 PNG 圖片。
當你在控制項中使用 nine-patch 圖片時(如 android:background="@drawable/button"),系統會依控制項大小自動拉伸你想拉伸的部分,效果如下圖:
Android 對話框彈出位置與透明度的實作詳解
預設的對話框總是顯示在畫面正中央,但實際開發中,我們常常需要調整它的位置(例如置於螢幕上方或下方),或是做出透明、半透明乃至背景模糊的效果。要達成這些客製化需求,關鍵在於
Android MediaPlayer 完整指南:播放控制、進度條與快轉倒轉
要在 App 裡播放音訊或影片,Android 提供了不少途徑,而 MediaPlayer 是最直覺的一個。它讓你以幾行程式碼就能控制內建媒體播放服務,完成播放、暫停、跳
從零打造 Android 天氣 App:網路連線、背景執行緒與 JSON 解析全流程
幾乎所有真實的 App 都要跟網路打交道,而「按下按鈕 → 發請求 → 拿 JSON → 解析 → 更新畫面」就是最典型的一條鏈路。本章用一個天氣查詢 App 把這條鏈路
動手做 Android 本地化:以英文與義大利文為例
App 的下一個使用者,可能住在米蘭、大阪或首爾。要讓他們覺得這個 App 是為自己做的,最基本的一步就是讓介面說他們的語言。Android 的資源系統把這件事變得相當直