Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 螢幕適配完整指南(學習筆記整理)

編輯:關於 Android 程式設計

做 Android 開發,遲早都要面對「適配」這兩個字。它不像新 API 那樣有明確的學習路徑,卻又貫穿每一個專案。這篇筆記把螢幕適配的成因、核心概念與落地手段梳理成一條線,適合反覆回看。

一、為什麼非得做螢幕適配

答案很直白:設備實在太多了。有廠商做過統計,支援 Android 的設備型號數量幾年之內就從三千多種膨脹到兩萬四千多種,而且每年還在往上翻。晶片、面板、系統定制各不相同的廠商共同拼出了一個高度碎片化的生態。

對開發者而言,碎片化意味著同一套介面在不同機器上可能呈現完全不同的觀感:文字擠成一團、按鈕跑到奇怪的位置、圖片被拉伸變形。適配工作的本質,就是讓介面在有限的幾類主流規格上都能保持可用且體面。

二、到底要適配哪些螢幕

理想很美好,現實是我們不可能為兩萬多種設備逐一調參。可行的做法是抓住主流分辨率——它們覆蓋了絕大多數用戶。

結論:把精力放在幾個主流分辨率上,其餘靠自適應機制兜底。

三、幾個必須搞清楚的基礎概念

1. 螢幕尺寸、螢幕分辨率、像素密度

螢幕尺寸指的是螢幕對角線的長度,單位是英寸(1 英寸 = 2.54 釐米)。它只描述「多大」,不涉及清晰度。

螢幕分辨率是橫縱方向上的像素點數量,單位 px,通常寫成「縱 × 橫」,例如 1920 × 1080。

像素密度則是每英寸分佈多少個像素點,單位 dpi(dot per inch)。它由尺寸和分辨率共同決定,計算方式是先對縱橫像素做平方和開根號得到對角線像素數,再除以對角線英寸長度。以 Nexus 5 為例:√(1920² + 1080²) ÷ 4.95 ≈ 445dpi。

2. px、dp/dip、dpi、sp 的關係

px:圖像的最小構成單位,一個 px 就是一個實體像素點。

dp / dip:Density Independent Pixels,密度無關像素。以 160dpi 為基準,此時 1dp = 1px。它的設計意圖是讓同一個尺寸在不同密度螢幕上的物理大小趨於一致。

sp:Scale-Independent Pixels,可隨系統「字體大小」設定縮放,主要用於文字。實踐中常用的值有 12sp、14sp、18sp、22sp。

3. mdpi、hdpi、xhdpi、xxhdpi、xxxhdpi

建立新專案時,Android Studio 會自動生成多套 drawable / mipmap 與 values 目錄,就是為了在不同密度下提供對應資源。各檔位的密度區間大致如下:

名稱像素密度範圍
mdpi120dpi – 160dpi
hdpi160dpi – 240dpi
xhdpi240dpi – 320dpi
xxhdpi320dpi – 480dpi
xxxhdpi480dpi – 640dpi

四、具體怎麼做

支援多種螢幕尺寸

(1)善用 wrap_content、match_parent 與 weight

先看寬度設為 0dp 搭配 weight 的寫法:

<button
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="button1" />
<button
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="2"
    android:text="button2" />

再對比寬度設為 match_parent 的情況:

<button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="button1" />
<button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="2"
    android:text="button2" />

兩張圖為什麼差這麼多?關鍵在 weight 的計算規則:

最終寬度 = 原始寬度 + 剩餘空間 × 權重百分比

設螢幕寬度為 L:

  • button1 使用 0dp:0 + L × 1/3 = 1/3 L
  • button1 使用 match_parent:L + (L − 2L) × 1/3 = 2/3 L

高度方向同理。理解這條公式,以後遇到權重相關的顯示異常基本都能自己算出結果。

(2)用相對布局,別用絕對布局

日常首選 LinearLayout、RelativeLayout、FrameLayout,絕對布局 AbsoluteLayout 早已廢棄,不要再用。

(3)使用限定符(尺寸限定符,API 3.2 之前)

以新聞閱讀器的雙面板為例,默認放單面板布局:

<!-- res/layout/main.xml -->
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <fragment
        android:id="@+id/headlines"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:name="com.example.android.newsreader.HeadlinesFragment" />
</LinearLayout>

平板(大於 7 英寸)走 layout-large 下的雙面板:

<!-- res/layout-large/main.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_width="400dp"
        android:layout_height="fill_parent"
        android:layout_marginRight="10dp"
        android:name="com.example.android.newsreader.HeadlinesFragment" />
    <fragment
        android:id="@+id/article"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:name="com.example.android.newsreader.ArticleFragment" />
</LinearLayout>

(4)使用最小寬度限定符(API 3.2 之後)

新方案用 sw600dp 這類限定符,判斷依據是螢幕寬或高中較短的一邊是否大於 600dp,比舊的 large / xlarge 更精準:

<!-- res/layout/main.xml:單面板(默認) -->
<!-- res/layout-sw600dp/main.xml:雙面板 -->

如果同時要相容 3.2 之前和之後,就得兩套限定符都維護。

(5)使用布局別名

當多個限定符下的布局內容完全相同時,可以用別名避免重複維護。先把雙面板的公共部分抽成 main_twopanes.xml,再用 setContentView(R.layout.main) 統一入口:

<!-- res/values/layout.xml:默認單面板 -->
<resources>
    <item name="main" type="layout">@layout/main</item>
</resources>

<!-- res/values-large/layout.xml:3.2 之前的平板 -->
<resources>
    <item name="main" type="layout">@layout/main_twopanes</item>
</resources>

<!-- res/values-sw600dp/layout.xml:3.2 之後的平板 -->
<resources>
    <item name="main" type="layout">@layout/main_twopanes</item>
</resources>

(6)使用螢幕方向限定符

同一台平板橫豎屏的可用寬度差很多,因此有必要再細分:

<!-- res/values-sw600dp-land/layouts.xml -->
<resources>
    <item name="main" type="layout">@layout/main_twopanes</item>
</resources>

<!-- res/values-sw600dp-port/layouts.xml -->
<resources>
    <item name="main" type="layout">@layout/main</item>
</resources>

把設備形態展開來看,就是這樣一張決策表:

設備形態布局
小螢幕,縱向單面板
小螢幕,橫向單面板
7 吋平板,縱向單面板 + 操作欄
7 吋平板,橫向雙面板(寬)+ 操作欄
10 吋平板,縱向雙面板(窄)+ 操作欄
10 吋平板,橫向雙面板(寬)+ 操作欄
電視,橫向雙面板(寬)+ 操作欄

對應的四個布局檔分別是 onepane.xml、onepane_with_bar.xml、twopanes.xml(左欄 400dp)、twopanes_narrow.xml(左欄 200dp),再用 values 下的 item 與 boolean 標誌把「當前用哪個布局」「是否為雙面板」傳給程式碼:

<!-- 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>

(7)使用自動拉伸位圖(.9 圖)

需要拉伸的容器背景,用 .9.png 指定可拉伸區與內容對齊區,比寫死尺寸靈活得多。

支援多種螢幕密度

1. 使用與密度無關的單位

文字用 sp,位置與尺寸用 dp。但這裡有個坑:

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorAccent">
    <Button
        android:layout_width="150dp"
        android:layout_height="match_parent"
        android:background="@android:color/holo_blue_dark" />
    <Button
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:background="@android:color/darker_gray" />
</RelativeLayout>

明明用了 dp,兩台設備的顯示效果還是不一致。原因在於各設備的可用寬度本身就不同,dp 只解決了密度差異,並沒有解決尺寸差異。

思路有兩條:一是在多套 values 目錄下為不同檔位提供不同的 dp 值(系統找不到對應資源時會回退到默認目錄,最終仍以 px 計,需要自己權衡);二是接受輕微誤差,把關鍵比例交給 weight 和百分比類屬性來控制。

2. 提供備用位圖

為不同密度準備匹配的圖片,這樣做既清晰又省內存。考慮到包體積,一般保留一套通用圖,只對 logo、啟動圖等重點資源多切幾套。

自適應 UI 的落地流程

  1. 確定當前使用的布局;
  2. 依當前布局做出對應響應;
  3. 復用其他 Activity 中的 Fragment;
  4. 妥善處理螢幕配置變化(旋轉等)。

五、實踐中的兩點提醒

一是設計稿尺寸:拿到高清水印圖後先確認基準密度,再換算成 dp,否則整齊的稿子在真機上會整體偏大或偏小。

二是動態設置:像 PopupWindow 這種位置敏感的元件,尺寸和偏移最好在代碼裡按實際螢幕參數計算,不要完全依賴 XML 寫死。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved