編輯:關於 Android 程式設計
設備碎片化是 Android 開發的老問題。本文從最基礎的佈局屬性講起,一路談到資源限定符與自動拉伸位圖,梳理出一套完整的螢幕適配思路。
要讓佈局具備足夠的彈性,首先應該用 "wrap_content" 和 "match_parent" 來控制視圖的寬高,而不是寫死具體數值。
· wrap_content:系統會把視圖尺寸設為容納內容所需的最小值。
· match_parent(API 8 以下稱 fill_parent):視圖會展開填滿父容器。
使用這兩個值,視圖要麼只佔用自身需要的空間,要麼自動撐滿可用空間。例如:
xmlns:android="https://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"
這樣寫的好處是:佈局能自動適應各種螢幕尺寸與方向。下圖展示同一份佈局在縱向與橫向下的差異,可見組件尺寸會隨螢幕寬高自動伸縮:
圖 1。縱向(左)與橫向(右)模式下的新聞閱讀器範例。
把 LinearLayout 層層嵌套,再配合 wrap_content 與 match_parent,確實能搭出相當複雜的佈局。但 LinearLayout 只能把子視圖排成一條直線,無法精確表達子視圖之間的特殊關係。當你需要「A 靠左、B 靠右」這類布局時,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_alignRight="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>
圖 2 是這份佈局在 QVGA(小螢幕)上的效果:
圖 2。QVGA 小螢幕截圖。
圖 3 則是同一份佈局在大螢幕上的效果:
圖 3。WSVGA 大螢幕截圖。
值得注意的一點是:雖然組件尺寸變了,但它們之間的相對位置關係依然保留,這一切都由 RelativeLayout.LayoutParams 描述。
彈性佈局能拉伸空間以適配各種螢幕,但「能顯示」不等於「體驗最佳」。因此除了靈活佈局,還應該針對不同螢幕配置準備備用佈局,透過配置限定符讓系統在運行時自動挑選合適的資源。
最典型的需求是「雙面板」模式:平板與電視螢幕夠大,可以左側列列表、右側顯示內容;手機則必須分頁展示。可以這樣組織文件:
· res/layout/main.xml——單面板(默認)佈局
· res/layout-large/main.xml——雙面板佈局
large 限定符會讓系統在 7 吋及以上的平板設備上選擇第二份佈局,其他設備則回退到無限定符的默認版本。
在 Android 3.2 之前的版本上,「較大」螢幕是一個相當粗糙的分類,會把戴爾 Streak、早期 Galaxy Tab 以及大部分 7 吋平板混為一談。但 5 吋與 7 吋設備往往需要完全不同的佈局,這正是 Android 3.2 引入最小寬度限定符的原因。
最小寬度限定符讓你能以 dp 為單位描述螢幕寬度。標準 7 吋平板的最小寬度約為 600 dp,因此可以這樣命名:
· res/layout/main.xml——單面板(默認)
· res/layout-sw600dp/main.xml——雙面板
也就是說,最小寬度 ≥ 600 dp 的設備會選用雙面板佈局,否則回退到單面板。需要注意的是,Android 3.2 以下的設備無法識別 sw600dp,因此仍需保留 res/layout-large/main.xml 作為相容方案。
由於最小寬度限定符僅支援 3.2 及以上,你往往不得不為同一份雙面板佈局準備兩份文件:
· res/layout/main.xml:單面板
· res/layout-large:多面板
· res/layout-sw600dp:多面板
後兩者內容完全相同,一個服務於 3.2 及以上,一個服務於更低版本的平板與電視。這種重複會帶來維護負擔,解法是使用別名文件。
先定義兩份實際佈局:
· 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。由於同時覆蓋了 large 與 sw600dp 兩個選擇器,無論 Android 版本高低,平板與電視都會命中正確的佈局。
同一塊螢幕在橫向與縱向下往往需要不同布局。以新聞閱讀器為例,各配置下的表現大致如下:
· 小螢幕縱向/橫向:單面板,帶徽標
· 7 吋平板縱向:單面板,帶操作欄
· 7 吋平板橫向:雙面板(寬),帶操作欄
· 10 吋平板縱向:雙面板(窄),帶操作欄
· 10 吋平板橫向/電視橫向:雙面板(寬),帶操作欄
這些佈局分別定義在 res/layout/ 下:
· onepane.xml、onepane_with_bar.xml、twopanes.xml、twopanes_narrow.xml
接著用別名把它們映射到各配置:
res/values/layouts.xml:
<resources>
<item name="main" type="layout">@layout/onepane_with_bar</item>
<item name="is_two_pane" type="bool">false</item>
</resources>
res/values-sw600dp-land/layouts.xml:
<resources>
<item name="main" type="layout">@layout/twopanes</item>
<item name="is_two_pane" type="bool">true</item>
</resources>
res/values-sw600dp-port/layouts.xml:
<resources>
<item name="main" type="layout">@layout/onepane</item>
<item name="is_two_pane" type="bool">false</item>
</resources>
res/values-large-land/layouts.xml:
<resources>
<item name="main" type="layout">@layout/twopanes</item>
<item name="is_two_pane" type="bool">true</item>
</resources>
res/values-large-port/layouts.xml:
<resources>
<item name="main" type="layout">@layout/twopanes_narrow</item>
<item name="is_two_pane" type="bool">true</item>
</resources>
支持多種螢幕尺寸,往往意味著圖片資源也得跟著變形。比如一個按鈕背景,無論按鈕被拉成什麼形狀都要好看。如果直接把普通圖片掛到可縮放的組件上,系統會在運行時均勻拉伸整張圖,邊框和圓角很快就會糊掉。
正確做法是使用自動拉伸位圖(NinePatch)——這是一種特殊格式的 PNG,用來標明哪些區域可以拉伸、哪些必須保持原樣。先準備一張普通圖片(圖 4 已放大 4 倍以便觀察):
圖 4。button.png
然後用 SDK 自帶的 draw9patch 工具(位於 tools/ 目錄)打開它,沿四條邊緣繪製黑色像素:
圖 5。button.9.png
規則是:頂部與左側邊界的黑像素標示可拉伸區域,右側與底部邊界的黑像素標示內容的放置區域。
還要注意 .9.png 這個擴展名——必須保留,系統框架正是靠它辨識 NinePatch 而非普通 PNG。
把它設為背景(android:background="@drawable/button")後,框架會按按鈕的實際尺寸智能拉伸:
圖 6。使用 button.9.png 的按鈕在多種尺寸下的呈現效果。
Android 錄音與音頻播放功能完整實現教程
本文記錄了一個錄製聲音文件並播放的完整 Demo 實現過程,供需要的朋友參考。 先說明幾點注意事項:其一,這個 Demo 未對「多次快速點擊同一個音頻文件」做完善處理,偶
用 ListView 瀏覽 SD 卡檔案:兩種做法與 BaseAdapter 完整實作
要做一個簡易的檔案總管,核心就是把一個目錄下的檔案列成清單,點進去還能繼續往下鑽。Android 裡有兩條路:一是直接繼承 ListActivity,二是自己準備 Bas
Android 計算器介面布局:以 LinearLayout 打造按鍵與顯示區
計算器是 Android 入門練習中很常見的介面題型。它通常包含一個顯示結果的輸入區,以及一組數字與運算按鈕。本文以 LinearLayout 為例,說明如何安排顯示區與
Android 接入騰訊開放平台實戰總結(一):QQ 登入與應用寶支付全流程
騰訊開放平台的接入,用過的人都懂:文檔分散、版本交叉、細節缺失。open.qq.com 上的資料又多又雜,社交類 API 更是琳瑯滿目。本文只聚焦最常見的兩塊——QQ 登