編輯:關於 Android 程式設計
適配大概是 Android 開發者共同的痛。Google 給了不少官方建議,可真要照著做把設備適配得乾乾淨淨,難度依然不低。作者此前寫過幾篇相關文章:
本文的靈感正是來自這幾篇,但無論是易用程度還是最終效果,都比它們更好。
先說前幾種思路的問題所在:
第一種:按設計圖尺寸把 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

效果基本一致。而它能帶走的是這些煩惱:
項目地址:https://github.com/hongyangAndroid/AndroidAutoLayout
dependencies {
compile project(':autolayout')
}
仍在用 Eclipse 的同學,只能複製源碼了。接著在 AndroidManifest 中聲明設計稿尺寸:
<meta-data android:name="design_width" android:value="768" /> <meta-data android:name="design_height" android:value="1280" />
讓 Activity 繼承 AutoLayoutActivity,並在 onCreate 中調用一次:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AutoLayout.getInstance().auto(this);
}
兩步接入,之後就可以按上一節的方式寫布局了。
如果不願意繼承 AutoLayoutActivity,也可以手動把系統容器換成對應的自動化版本:
所有原有屬性都仍然支持,只是根布局不會再享受 px 自動百分比化——不過根布局通常寫 MATCH_PARENT,影響不大。
寫布局不能實時預覽,效率會大打折扣。用法是:記住設計稿尺寸(如 768×1280),然後在 PreView 面板裡選擇分辨率一致的設備。

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

兩點提醒:設計稿尺寸若不在預設設備列表裡,自己新建一個即可;不要在 PreView 中切換各種分辨率去檢查適配,因為部分計算發生在運行時,預覽是看不出效果的。
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 的百分比化思路,仍有助於看清「適配」這件事的本質。
Android Socket 聊天軟件之聊天氣泡佈局與動態加載實作
前面已經談過 IQ 聊天軟體的區域網路聊天測試,這次來拆解氣泡的生成與訊息列表的動態加載。這不是引用第三方元件,而是自己整合佈局與容器完成的——看完源碼你會發現原理並不複
Android 項目基本結構詳解(資源、Assets 與權限管理)
一、簡介 第 3 章雖然透過百度地圖應用展示了你可能感興趣的內容,但對初學者來說,一開始就要看懂並理解程式碼可能非常吃力。為了解決這個問題,從這一章開始,本模塊將從最基本
Android 圓角與圓形圖片的四種實現方案匯總
圓角邊框和圓形頭像幾乎每個項目都會用到,實現途徑卻不止一種。下面把四種常見做法放在一起對比,方便按場景選用。 先看四種方案的效果: 方案一:shape 資源畫圓角背景
Android Studio 插件 GsonFormat:快速把 JSON 轉成 Java Bean
概述 在 Android 開發中,伺服器回傳的 JSON 往往具有多層嵌套結構。若每次都要手寫對應的 Java Bean,不僅費時,還很容易因為欄位名稱、型別或嵌套層級而