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

Android AutoLayout:寫布局直接抄設計稿,堪稱適配終結者

編輯:關於 Android 程式設計

一、屏幕適配,為什麼這麼難

適配大概是 Android 開發者共同的痛。Google 給了不少官方建議,可真要照著做把設備適配得乾乾淨淨,難度依然不低。作者此前寫過幾篇相關文章:

  • Android 屏幕適配方案
  • Android 百分比布局庫(percent-support-lib)解析與擴展
  • Android 增強版百分比布局庫:為了適配而擴展

本文的靈感正是來自這幾篇,但無論是易用程度還是最終效果,都比它們更好。

先說前幾種思路的問題所在:

第一種:按設計圖尺寸把 px 換算成百分比,為各主流屏幕生成對應數值,每種屏幕一個 values 文件夾。缺點是文件夾爆炸,且遇到特殊尺寸就抓瞎(還得手寫一份默認文件夾兜底)。

第二、三種:基於 Google 的百分比布局,確實解決了大半問題,但使用體驗「反人類」——設計圖標的是 px,你得自己算百分比,而百分比又依賴父容器尺寸,設計稿往往不會把每層父容器都標出來。另外因為依賴父容器,ScrollView、ListView 內部的高度根本没法用百分比。

所以理想中的適配應該是這樣:

拿到設計圖,在 meta 信息裡填好設計稿尺寸,布局直接抄稿上的數字,不做任何額外換算,不產生多餘資源文件,各種分辨率一次搞定。

二、直觀體驗

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

「新增旅客」這一塊,布局可以照抄如下:

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

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

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

Item 布局同理:

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

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

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

看到這裡,最大的疑問大概是:你用的可是 px,px 怎麼可能完成適配?

關鍵在於:這裡的 px 並不代表 1 個物理像素。庫內部會把它換算成百分比——設計稿高 720px,你寫 72px 就是佔 10%;這份布局跑到任何分辨率的機器上,72px 永遠代表 10% 的高度。這就是適配的原理。

對比一下兩台差距極大的設備:

768×1280,Android 4.4.4

480×800,Android 2.3.7

效果基本一致。而它能帶走的是這些煩惱:

  • 不用再盯著設計稿反覆琢磨控件該取多少 dp;
  • 不用再為一堆屏幕各寫一份 dimens;
  • 不用再手算百分比;
  • 不用再跟 UI 和產品MM解釋 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,也可以手動把系統容器換成對應的自動化版本:

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

所有原有屬性都仍然支持,只是根布局不會再享受 px 自動百分比化——不過根布局通常寫 MATCH_PARENT,影響不大。

四、注意事項

(1)如何正確使用 PreView

寫布局不能實時預覽,效率會大打折扣。用法是:記住設計稿尺寸(如 768×1280),然後在 PreView 面板裡選擇分辨率一致的設備。

這樣才能看到最準確的預覽效果:

兩點提醒:設計稿尺寸若不在預設設備列表裡,自己新建一個即可;不要在 PreView 中切換各種分辨率去檢查適配,因為部分計算發生在運行時,預覽是看不出效果的。

(2)關於 TextView

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

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

運行起來高度絕不會剛好 32px,文字上下都有留白;而如果寫死高度,又容易出現文字被裁切。應對辦法是:對字體尺寸要求精確的地方,把上下 margin 適當寫小一點來補償。

另外,雖然用 margin_left、margin_top 也能湊出居中,但為了精準,居中場景更推薦 gravity、centerInParent 這類屬性。

五、其他收益

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

性能與結構上的改進:因為位置可以靠 margin 精確控制,嵌套層數能大幅削減,複雜界面做到零嵌套並不難。RelativeLayout 的 toRightOf 之類規則基本可以用 marginLeft 取代;LinearLayout 的 weight 也幾乎用不上——屏幕寬 720px 要四等分,直接寫 layout_width="180px" 就行。

需要補充的是,這類方案屬於特定歷史階段的產物;在今天的項目裡,ConstraintLayout 配合 dp/sp 與約束佈局已經是更主流的選擇。理解 AutoLayout 的百分比化思路,仍有助於看清「適配」這件事的本質。

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