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

Android 屏幕適配五種方案詳解:從像素密度到權重布局

編輯:關於 Android 程式設計

什麼是屏幕適配

所謂適配,就是讓同一個應用在不同的手機上呈現一致的視覺效果。動手之前,第一步永遠是先確認當前設備屬於哪一檔像素密度(xhdpi、hdpi、mdpi 等)。本文以華為 G700 與模擬器為例,示範密度計算與五種常見適配手段。

像素密度怎麼算

先求出屏幕對角線上的像素總數,再除以對角線英寸數:

案例一:G700
分辨率 1280*720,屏幕 5 英寸
sqrt(1280*1280 + 720*720) / 5 ≈ 293.72dpi
最接近 320dpi,歸入 xhdpi,1dp = 2px

案例二:模擬器
分辨率 800*480,屏幕 3.7 英寸
sqrt(800*800 + 480*480) / 3.7 ≈ 252.15dpi
最接近 240dpi,歸入 hdpi,1dp = 1.5px

對照 Google 官方文檔,市場上的設備大致可分為五檔:

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

方案一:圖片適配

不同密度的設備加載 res 下不同文件夾的圖片資源。

<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 目錄(如 values-1280x720、values-800x480),各自定義同名尺寸值。

<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:layout_width="@dimen/width"
        android:layout_height="wrap_content"
        android:background="#987654"
        android:text="@string/hello_world" />
</LinearLayout>

模擬器(hdpi,values-800x480):<dimen name="width">160dp</dimen>,換算後為 240px。

G700(xhdpi,values-1280x720):<dimen name="width">180dp</dimen>,換算後為 360px。兩者都能讓控件落在屏幕中間位置。

G700(xhdpi)效果:

模擬器(hdpi)效果:

方案三:布局文件適配

為不同分辨率準備不同的 layout 目錄,例如 layout-1280x720 與 layout-800x480,各放一份 activity_main.xml。

模擬器(res/layout-800x480/activity_main.xml):
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="800*480 手機會加載的布局" />

G700(res/layout-1280x720/activity_main.xml):
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="1280*720 手機會加載的布局" />

G700(xhdpi)效果:

模擬器(hdpi)效果:

方案四:Java 代碼動態計算

透過 API 拿到屏幕的實際寬高,再按比例設定控件的 LayoutParams。

TextView tv = (TextView) findViewById(R.id.tv);

// 封裝屏幕信息的對象
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);

Constant.srceenHeight = metrics.heightPixels;
Constant.srceenWidth  = metrics.widthPixels;

// 寬高各佔 50%
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)效果:

方案五:權重(weight)適配

借助 LinearLayout 的剩餘空間分配能力,讓子控件按比例瓜分屏幕,這是列表與分欄布局裡最省心的做法。

<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:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#000000" />

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

G700(xhdpi)效果:

模擬器(hdpi)效果:

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