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

Android 螢幕適配完整解析:像素密度換算與五種常見適配手法

編輯:關於 Android 程式設計

先搞清楚什麼叫「適配」

所謂適配,就是同一套 App 裝在不同手機上,畫面的比例、元素大小與排版位置都能維持一致的水準。動手之前必須先確認裝置屬於哪一種像素密度等級(xhdpi、hdpi、mdpi 等),算出它的實際密度,再依比例決定介面元素的位置與尺寸。

第一步:把 dpi 算出來

dpi 指的是每英寸長度上分佈的像素點數量。手機螢幕是一塊矩形,兩條直角邊分別排列著橫向與縱向的像素點,斜邊長度就是我們常說的「幾英寸」。用勾股定理就能反推密度:

sqrt(寬² + 高²) / 對角線英寸數 = dpi

案例一(G700)

解析度 1280 × 720,螢幕 5 英吋
sqrt(1280×1280 + 720×720) / 5 ≈ 293.72dpi
依 Google 官方文件的取整規則,最接近 320dpi,
因此歸類為 xhdpi,換算關係為 1dp = 2px

案例二(模擬器)

解析度 800 × 480,螢幕 3.7 英吋
sqrt(800×800 + 480×480) / 3.7 ≈ 252.15dpi
最接近 240dpi,因此歸類為 hdpi,
換算關係為 1dp = 1.5px


五個密度等級與 dp/px 換算表

套用上法,市面上大多數裝置都能落進這五個等級:

  • ldpi:120dpi,1dp = 0.75px
  • mdpi:160dpi,1dp = 1px
  • hdpi:240dpi,1dp = 1.5px
  • xhdpi:320dpi,1dp = 2px
  • xxhdpi:480dpi,1dp = 3px

以下實測皆以 G700(xhdpi)與 800×480 模擬器(hdpi)這兩款裝置為對照。

適配方式一:圖片資源適配

核心思路是「讓系統自己挑圖」。不同密度的裝置會去載入 res 下對應的資源目錄,例如 drawable-hdpi、drawable-xhdpi,同一張圖只需準備多份不同解析度的版本即可。

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/a" />
</RelativeLayout>

G700(xhdpi)會載入 res/drawable-xhdpi 下的 a.jpg:

模擬器(hdpi)則載入 res/drawable-hdpi 下的 a.jpg:

適配方式二:dimens.xml 尺寸適配

在 res 下建立多組 values-1280x720 這類限定符目錄,各自放一份 dimens.xml,並在布局中引用 @dimen/width。系統會依當前裝置的解析度挑選對應的那一份。

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:background="#987654"
        android:layout_width="@dimen/width"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
</LinearLayout>

模擬器(hdpi)載入 res/values-800x480/dimens.xml:

<resources>
    <dimen name="width">160dp</dimen>
</resources>
// 1dp = 1.5px,160dp = 240px

G700(xhdpi)載入 res/values-1280x720/dimens.xml:

<resources>
    <dimen name="width">180dp</dimen>
</resources>
// 1dp = 2px,180dp = 360px

G700(xhdpi)實測:

模擬器(hdpi)實測:

適配方式三:布局文件適配

做法更直接:為不同解析度準備整套布局,例如 layout-1280x720、layout-800x480,讓系統自動選擇。

模擬器(hdpi)載入 res/layout-800x480/activity_main.xml:

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="800*480 手機會載入的布局" />
</RelativeLayout>

G700(xhdpi)載入 res/layout-1280x720/activity_main.xml:

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1280*720 手機會載入的布局" />
</RelativeLayout>

G700(xhdpi)實測:

模擬器(hdpi)實測:

適配方式四:Java 代碼動態計算

透過 API 取得當前螢幕的實際像素寬高,再按比例設定控件的 LayoutParams,靈活度最高。

// 布局
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/tv"
        android:background="#000000"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
</RelativeLayout>

// Activity 中的核心邏輯
TextView tv = (TextView) findViewById(R.id.tv);
// 取得封裝螢幕資訊的物件
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
Constant.srceenHeight = metrics.heightPixels;
Constant.srceenWidth = metrics.widthPixels;
// 寬高各佔五成
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
        (int) (Constant.srceenWidth * 0.5 + 0.5),
        (int) (Constant.srceenHeight * 0.5 + 0.5));
tv.setLayoutParams(lp);

G700(xhdpi)實測:

模擬器(hdpi)實測:

適配方式五:權重(layout_weight)適配

把剩餘空間交給系統按比例分配,是實務上最省事、也最推薦的做法。寬度設為 0dp 搭配 layout_weight,各子 View 就會依權重瓜分父容器。

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".MainActivity">

    <TextView
        android:background="#000000"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent" />

    <TextView
        android:background="#123456"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent" />
</LinearLayout>

G700(xhdpi)實測:

模擬器(hdpi)實測:

怎麼選

圖片適配只解決「圖」的問題,dimens 與 layout 限定符方案維護成本會隨裝置組合線性增加,真實專案裡很難窮舉。實務上最穩健的組合是:以 dp/sp 作為基本單位,搭配權重與 match_parent 完成主要排版,再用 Java 動態計算處理少數需要精準比例的場景。這樣既能覆蓋大多數機型,也不必為每種解析度準備一整套資源。

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