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

AutoLayout:直接照抄設計稿尺寸的 Android 適配方案

編輯:關於 Android 開發

適配大概是 Android 開發者共同的痛。Google 給了一堆建議,但真要只靠那些建議把各種螢幕處理得漂亮,仍然不容易。本文介紹一個思路很不一樣的方案:AutoLayout。

一、緣起

先回顧幾種常見做法及其問題:

方案 A:為每個螢幕生成對應的 dimens——把設計稿上的 px 換算成百分比,為各主流螢幕各產生一份 values。問題是產生大量檔案,且遇到特殊尺寸時必須另外建立預設資料夾。

方案 B:Google 的百分比版面(percent-support-lib)——已大幅改善適配,但使用體驗偏彆扭:設計稿標的都是 px,你得自己算百分比,而百分比又依賴父容器尺寸,設計稿未必每層都標。另外 ScrollView、ListView 內部的子元素高度也無法用百分比。

所以理想中的適配應該是:拿到設計稿,在 meta 資訊裡填一次尺寸,之後版面直接抄設計稿上的數字,不必額外計算,也不產生任何多餘資源檔。

二、實際操作感受一下

假設拿到這樣一張設計稿:

「新增旅客」這一列,版面可以這樣寫:

<RelativeLayout
    android:background="#ffffffff"
    android:layout_height="86px"
    android:layout_marginTop="26px"
    android:layout_width="match_parent">

    <ImageView
        android:id="@+id/id_tv_add"
        android:layout_gravity="center_vertical"
        android:layout_height="34px"
        android:layout_marginLeft="276px"
        android:layout_marginTop="26px"
        android:layout_width="34px"
        android:src="@mipmap/add" />

    <TextView
        android:layout_centerVertical="true"
        android:layout_height="wrap_content"
        android:layout_marginLeft="26px"
        android:layout_toRightOf="@id/id_tv_add"
        android:layout_width="wrap_content"
        android:text="新增旅客"
        android:textColor="#1fb6c4"
        android:textSize="32px" />

</RelativeLayout>

對比效果:

列表項的寫法同理:

<RelativeLayout
    android:background="#ffffffff"
    android:layout_height="108px"
    android:layout_marginTop="26px"
    android:layout_width="match_parent">

    <TextView
        android:layout_height="wrap_content"
        android:layout_marginLeft="22px"
        android:layout_marginTop="16px"
        android:layout_width="wrap_content"
        android:text="王大炮"
        android:textColor="#333"
        android:textSize="28px" />

    <TextView
        android:layout_alignParentBottom="true"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16px"
        android:layout_marginLeft="22px"
        android:layout_width="wrap_content"
        android:text="護照:G50786449"
        android:textColor="#999"
        android:textSize="26px" />

</RelativeLayout>

三、px 怎麼能完成適配?

關鍵在於:這裡的 px 並不代表一個實體像素。庫在內部會把它換算成百分比——假設設計稿高 1280px,你填 128px 就代表 10% 的高度;這份版面跑在任何解析度的手機上,128px 永遠都是 10%。這就是它的適配原理。

來看兩台解析度差距明顯的裝置:

768*1280,Android 4.4.4

480*800,Android 2.3.7

帶來的具體改變:

  • 不用再對著設計稿猜這個控件的寬高該填多少 dp;
  • 不用再為多種螢幕維護多份 dimens;
  • 不用再手算百分比;
  • 不用再跟 UI 設計解釋什麼是 dp。

專案位址:https://github.com/hongyangAndroid/AndroidAutoLayout

四、使用方式

1. 註冊設計稿尺寸

dependencies {
    compile project(':autolayout')
}

Eclipse 用戶只能複製原始碼使用。接著在 AndroidManifest 中標明設計稿尺寸:

<meta-data android:name="design_width"  android:value="768" />
<meta-data android:name="design_height" android:value="1280" />

2. 在 Activity 中開啟

讓 Activity 繼承 AutoLayoutActivity,並在 onCreate 中呼叫:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    AutoLayout.getInstance().auto(this);
}

若不繼承 AutoLayoutActivity,就得像用 Google 百分比庫那樣,把手動把容器換成對應版本:

  • LinearLayout → AutoLinearLayout
  • RelativeLayout → AutoRelativeLayout
  • FrameLayout → AutoFrameLayout

原有的系統屬性都支援,但根版面就無法享受 px 自動百分比化。不過根版面通常都是 MATCH_PARENT,影響不大;而繼承 AutoLayoutActivity 的方式連根版面也能百分比化。

五、注意事項

1. 如何正確使用 Preview

先記住設計稿尺寸(例如 768 * 1280),然後在 Preview 面板選擇解析度一致的裝置:

這樣才能看到最精確的預覽:

兩點提醒:如果設計稿尺寸不在預設清單裡,自行新增一個裝置即可;另外不要在 Preview 裡逐一檢視所有解析度,因為部分計算是在執行期動態完成的,看不出適配效果。

2. 關於 TextView

TextView 的高度會與設計稿有些出入,這不是庫的問題,而是它與生俱來的特性:

<TextView
    layout_height="wrap_content"
    textSize="32px" />

執行後你會發現它不是 32px 高,文字上下自帶一些間距;若把高度寫死,又會發現文字顯示不全。

因應方式:對字體標註精確的 TextView,上下 margin 稍微寫小一點(本文的 Item 範例就有意縮小了 marginTop)。其他控件沒有這個問題。

另外,雖然可以用 margin_left / margin_top 硬算出居中,但為了精準,建議還是用 gravity、centerInXXX 這類屬性來做居中。

六、其他

支援的屬性

layout_width、layout_height、layout_margin(left/top/right/bottom)、padding(left/top/right/bottom)、textSize。不影響系統其他屬性,也不影響 dp、sp 的使用。

效能上的意外收穫

因為大量使用 margin 就能精準控制位置,版面嵌套可以大幅減少,複雜介面幾乎能做到零嵌套。RelativeLayout 的 rightOf 這類規則基本可由 marginLeft 取代,LinearLayout 的 weight 也幾乎不需要——螢幕寬 720px 要四等分,直接寫 layout_width=180px 就好。

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