編輯:關於 Android 開發
很多開發者一聽到 Android 終端的螢幕尺寸五花八門、解析度千奇百怪,就直覺認為適配必定是場噩夢。之所以有此顧慮,是因為在 Android 問世之前,大家熟悉的 UI 方案大致只有兩類:一是為每種解析度各出一套圖與版面,二是靠程式在執行期硬算座標。這兩條路放在碎片化嚴重的 Android 陣營裡,都難以收拾。
Android 提供的解法是「布局 + 9-patch」:讓版面規則由容器描述,圖片能拉伸的部分交給 9-patch 處理,密度則由 dp 與密度資源限額來吸收差異。
SDK 剛問世時,Android 提供了 AbsoluteLayout、FrameLayout、LinearLayout、RelativeLayout 與 TableLayout 五大基本布局。但 AbsoluteLayout 很快被認定是錯誤示範,在 Android 1.5 就不再支援;TableLayout 雖仍可用,隨著 Fragment 與 TabLayout 的普及,實際能派上用場的場合越來越少。因此真正需要熟練的,其實只剩 FrameLayout、LinearLayout 與 RelativeLayout 這三個。
這是最簡單的布局。其中的元素預設都以容器的左上角為基準,一層層疊上去,後加入的蓋住先加入的。
<FrameLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View android:layout_width="200dp" android:layout_height="200dp" android:background="#ff0000"/>
<View android:layout_width="200dp" android:layout_height="200dp" android:background="#000000"/>
<View android:layout_width="200dp" android:layout_height="200dp"
android:layout_margin="100dp" android:background="#00ff00"/>
</FrameLayout>
三個方塊層層相疊,只有最後那個因為設了 margin 而錯位露出,一眼就能看出「後者蓋前者」的規則。
若想指定位置,加上 android:layout_gravity:
<View android:layout_width="200dp" android:layout_height="200dp"
android:layout_gravity="bottom|right"
android:background="#000000"/>
設了 bottom|right 之後,這個 View 就被推到 FrameLayout 的右下角。
線性布局把子元素排成一條線,方向由 android:orientation 決定,對齊則由 android:gravity 控制。
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<View android:layout_width="100dp" android:layout_height="100dp" android:background="#ff0000"/>
<View android:layout_width="100dp" android:layout_height="100dp" android:background="#000000"/>
<View android:layout_width="100dp" android:layout_height="100dp" android:background="#00ff00"/>
</LinearLayout>
設為縱向且整體置中,三個方塊便垂直排列並在螢幕中央對齊。實務上最常用的是搭配 android:layout_weight 做比例分配,這在適配不同螢幕時特別好用。
相對布局允許子元素之間彼此參照定位,比線性布局靈活,也更貼近設計稿的思維。
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View android:id="@+id/red"
android:layout_width="100dp" android:layout_height="100dp" android:background="#ff0000"/>
<View android:id="@+id/black"
android:layout_width="100dp" android:layout_height="100dp"
android:layout_toRightOf="@id/red" android:layout_below="@id/red"
android:background="#000000"/>
<View android:id="@+id/green"
android:layout_width="100dp" android:layout_height="100dp"
android:layout_below="@id/black" android:layout_alignParentRight="true"
android:background="#00ff00"/>
<View android:id="@+id/gray"
android:layout_width="100dp" android:layout_height="100dp"
android:layout_centerInParent="true" android:background="#888888"/>
<View android:id="@+id/orange"
android:layout_width="100dp" android:layout_height="100dp"
android:layout_toRightOf="@id/green" android:layout_below="@id/gray"
android:background="#ff8800"/>
</RelativeLayout>
先給每個 View 一個 id(red、black、green、gray、orange),再用相對屬性描述彼此關係:black 位於 red 的右側與下方,green 在 black 下方並與父容器右對齊,gray 在父容器中置中,orange 則同時位於 green 右側與 gray 下方。這正是相對布局的典型用法。
布局本身的語法並不難,真正的功課在於「用最少的嵌套層數表達同一個畫面」——層級越淺,measure 與 layout 的開銷越低,這也是適配之外另一個必須在意的效能課題。
更多內容歡迎查看本人部落格:林彥君的部落格
Android 大圖載入記憶體:inSampleSize 取樣與螢幕自適應
直接把一張手機拍的高解析照片塞進 ImageView,是新手最常踩的 OutOfMemoryError 陷阱。本文示範如何用 BitmapFactory.Options
Android加固教程:先學會破解,用IDA動態調試修改內存改寫程式走向
Android加固教程:先學會破解,用IDA動態調試修改內存改寫程式走向 【版權所有,轉載請註明出處。】 因公司專案需要對app進行加固,經過數月研究,我實現了一套完整的
用 ListView 搭配 ViewPager 打造新聞 App 首頁
用 ListView 搭配 ViewPager 打造新聞 App 首頁 成品預覽 這次要實作的是一個類似新聞 App 的資訊流首頁:頂端是一張可自動輪播的大圖(用 Vi
Linux 0.11 內核剖析:分頁記憶體管理的初始化
Linux 0.11 內核剖析:分頁記憶體管理的初始化 【版權所有,轉載請註明出處。出處:https://www.cnblogs.com/joey-hua/p/55977