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


套用上法,市面上大多數裝置都能落進這五個等級:
以下實測皆以 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:

在 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)實測:

透過 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)實測:

把剩餘空間交給系統按比例分配,是實務上最省事、也最推薦的做法。寬度設為 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 動態計算處理少數需要精準比例的場景。這樣既能覆蓋大多數機型,也不必為每種解析度準備一整套資源。
魅族MX6跑分多少?Geekbench 曝光的多核表現與規格
魅族MX6在真機圖頻繁曝光之後,正式現身 Geekbench 跑分網站,發表時間也確定為 7 月 19 日。對於評估它的效能定位,跑分資料提供了第一個可參考的依據。跑分資
Android SwipeRefreshLayout 下拉刷新源碼解析
本文通過實例梳理 SwipeRefreshLayout 的用法與源碼要點,供大家參考。 SwipeRefreshLayout 是 Google 在 support v4
動手做一個 Android 注解框架:用反射與自訂註解消滅 findViewById
前言 在我們的專案裡,幾乎每天都在跟一些固定樣板打交道。比如在 Activity 中你得寫 findViewById(int) 去找控制項,這樣的程式碼對一個稍有經驗的開
achartengine 與 MPAndroidChart 接入指南:折線圖與餅圖代碼實操
Android 上做圖表,自己從零實現工作量極大,開源方案是理性選擇。本文聚焦代碼層面,把 achartengine 與 MPAndroidChart 的接入過程完整走一