編輯:Android 開發實例
Android 4.0 的 SDK 正式發布後,除了 UI 與 API 上的更新,對開發者最有感的莫過於兩個全新的佈局元件:Space 與 GridLayout。它們解決了過去必須靠多層嵌套才能勉強完成的對齊問題,也讓畫面結構變得更扁平、更有效率。本文就從 LinearLayout 的痛點出發,帶你看懂 GridLayout 的定位邏輯與常用屬性。
在 Android 的世界裡,最常被使用的佈局莫過於 LinearLayout。它讓子元件沿著單一軸線(水平或垂直)排列,簡單直覺。但一旦畫面變得複雜,開發者往往只能不斷巢狀堆疊 LinearLayout,而且還得小心嵌套層數不能太深。
關於巢狀佈局的缺點,社群早有歸納,大致可分為三類:
· 無法同時在 X 軸與 Y 軸方向上對元件做對齊。
· 多層嵌套會带来額外的測量與配置開銷,形成效能問題。
· 對支援自由拖曳編輯的視覺化佈局工具而言,結構不夠穩定。
最典型的第一個問題,可以從一個常見的電子郵件設定畫面看出來:「Email address」這個文字標籤,一方面希望與右側輸入框的下緣基線對齊,另一方面又要與下方欄位的右邊緣對齊。這種「同時跨兩軸」的需求,用巢狀 LinearLayout 幾乎無法優雅達成——於是 GridLayout 登場了。
GridLayout 把容器切成虛線構成的列(row)、欄(column)與儲存格(cell)。同一個元件可以同時參與「某一列的基線對齊」與「某一欄的靠右對齊」,前述的兩軸對齊難題就自然消解了。
值得一提的是,GridLayout 的 API 風格與 LinearLayout 相當接近,只是把標籤與屬性換了一套,因此既有經驗的開發者幾乎零成本上手。SDK 中附帶的範例位於:
samples/ApiDemos/src/com/example/android/apis/view/GridLayout0.java samples/ApiDemos/res/layout/grid_layout_1.xml
<?xml version="1.0" encoding="utf-8"?>
<GridLayout
xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:useDefaultMargins="true"
android:alignmentMode="alignBounds"
android:columnOrderPreserved="false"
android:columnCount="4">
<TextView
android:text="Email setup"
android:textSize="32dip"
android:layout_columnSpan="4"
android:layout_gravity="center_horizontal" />
<TextView
android:text="You can configure email in just a few steps:"
android:textSize="16dip"
android:layout_columnSpan="4"
android:layout_gravity="left" />
<TextView
android:text="Email address:"
android:layout_gravity="right" />
<EditText android:ems="10" />
<TextView
android:text="Password:"
android:layout_column="0"
android:layout_gravity="right" />
<EditText android:ems="8" />
<Space
android:layout_row="4"
android:layout_column="0"
android:layout_columnSpan="3"
android:layout_gravity="fill" />
<Button
android:text="Next"
android:layout_row="5"
android:layout_column="3" />
</GridLayout>
這段程式碼有幾個關鍵點值得注意:
· GridLayout 中通常不需要再依賴 WRAP_CONTENT / MATCH_PARENT 那套寫法,元件會依儲存格與 gravity 自動配置。
· 大多數元件並未顯式指定位置,而是靠排列順序加上 layout_columnSpan、layout_rowSpan 決定佔用的欄數與列數。
· android:columnCount="4" 宣告了四欄;欄的編號由左至右從 0 開始,列的編號由上而下同樣從 0 起算。因此畫面最下方的按鈕以 layout_row="5" 與 layout_column="3" 定位,也就是第 6 列、第 4 欄。
· Space 是 4.0 才加入的輕量元件,它不繪製任何內容,只用來在畫面中撐出一塊空白區域,取代過去常見的「佔位 TextView」或空白 View 技巧。
<GridLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="4"
android:rowCount="4">
<TextView android:text="1,1" />
<TextView android:text="1,2" />
<TextView android:text="1,3" />
<TextView android:text="1,4" />
<TextView android:text="2,1" />
<TextView android:text="2,2" />
<TextView android:text="2,3" />
<TextView android:text="2,4" />
<TextView android:text="3,1" />
<TextView android:text="3,2" />
<TextView android:text="3,3 longer" />
<TextView android:text="3,4" />
<TextView android:text="4,1" />
<TextView android:text="4,2" />
<TextView android:text="4,3" />
<TextView android:text="4,4" />
</GridLayout>
預設情況下,GridLayout 以水平方向填格:由左至右、由上而下。也就是「1,2」落在第 1 列第 2 欄。此時若某個儲存格內容特別長(例如「3,3 longer」),它會依內容寬度自行伸展,看起來與其他元件沒有對齊。要讓整欄等寬對齊,可以為該欄的元件加上 android:layout_gravity="fill_horizontal":
<TextView
android:layout_gravity="fill_horizontal"
android:text="1,3" />
...
<TextView
android:layout_gravity="fill_horizontal"
android:text="3,3 longer" />
若希望改成先由上而下填格,只要加上 android:orientation="vertical":
<GridLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnCount="4"
android:rowCount="4"
android:orientation="vertical" />
這時「1,2」會出現在「1,1」的下方。這裡有個容易踩到的細節:一旦你對某個元件顯式指定了 layout_row 或 layout_column,後續未指定位置的元件會順著被推擠位移。例如:
<TextView
android:layout_gravity="fill_horizontal"
android:layout_row="1"
android:text="1,3" />
由於 layout_row 從 0 起算,指定為 1 就是第 2 列,其他元件的位置也會跟著偏移。
兩者常被混淆,其實分工清楚:layout_gravity 決定元件「在儲存格中如何伸展或靠齊」,gravity 決定「元件內部的內容如何對齊」。下面的例子讓「1,3」跨兩列並填滿:
<TextView
android:layout_gravity="fill_horizontal"
android:layout_rowSpan="2"
android:text="1,3" />
若還想讓文字在儲存格正中央,再加上 android:gravity="center」即可:
<TextView
android:layout_gravity="fill"
android:gravity="center"
android:layout_rowSpan="2"
android:text="1,3" />
最後我們用「圖示 + 兩行文字 + 箭頭」這個常見的列表列樣式,分別以 LinearLayout 與 GridLayout 實作:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Line 1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Line 2" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=">" />
</LinearLayout>
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="3"
android:rowCount="2">
<ImageView
android:layout_gravity="fill"
android:layout_rowSpan="2"
android:src="@drawable/ic_launcher" />
<TextView
android:layout_gravity="fill"
android:text="Line 1" />
<TextView
android:layout_gravity="fill_vertical"
android:layout_rowSpan="2"
android:gravity="fill"
android:text=">" />
<TextView
android:layout_gravity="fill"
android:text="Line 2" />
</GridLayout>
</LinearLayout>
對照之下差異明顯:GridLayout 版本只有一層,不需要為了對齊而反覆巢狀化,撰寫與維護都更直觀,配置階段的負擔也更輕。
GridLayout 以簡潔的行列模型,替開發者省下了大量巢狀巢狀堆疊的功夫,在需要表格化、對齊要求高的畫面上尤其合適。遇到這類需求時,不妨優先考慮它,避免不必要的多層嵌套。在當時它僅支援 Android 4.0 以上版本,若要向下相容,則需額外引入支援庫(AndroidX GridLayout 即為此而生的後續方案)。
Android 地圖開發入門:MapView、API Key 與地圖模式切換
Google Maps 提供矢量地圖、衛星影像與地形圖三種視圖,在 Android 上則被拆成兩組能力:負責「畫地圖」的地圖 API,與負責「知道我在哪」的定位 API。
在電腦上使用 Android 模擬器安裝 APK 的完整流程
若想在電腦上執行 Android 應用,可以透過 Android SDK 提供的模擬器與 adb 工具完成安裝。整個流程可拆為環境設定、啟動模擬器、執行安裝三個步驟。設定
取得 SD 卡與機身儲存空間的總容量與可用大小
很多新手不知道,Android 早就把「查詢儲存空間」這件事包好了。不必自己去解析檔案系統、也不必硬算位元組,靠 StatFs 加上 Formatter 兩個類就能乾淨地
重力感應入門:用加速度感測器讀取三軸數值
Android 裡很多遊戲都用到重力感應,值得花點時間理解它的運作方式。 先建立座標觀念 以螢幕左下角為原點,箭頭指向為正方向,範圍從 -10 到 10,以浮點數為刻度