編輯:關於 Android 程式設計
布局是 Android 介面的骨架,選對容器能省下大量巢狀與效能調校的工夫。以下用四個可運行的小例子,把 LinearLayout、RelativeLayout、GridLayout 與 TableLayout 的特性直接呈現出來。
最直覺的容器,用 orientation 決定排列方向,用 layout_weight 分配剩餘空間。
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button android:id="@+id/btn1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="1" />
<Button android:id="@+id/btn2" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="2" />
<Button android:id="@+id/btn3" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="3" />
<Button android:id="@+id/btn4" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="4" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button android:id="@+id/btn5" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="4" />
<Button android:id="@+id/btn6" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="5" />
<Button android:id="@+id/btn7" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_weight="1"
android:text="6" />
</LinearLayout>
</LinearLayout>
結果:

注意第二組的第三顆按鈕設了 weight=1,因此它會佔滿該列剩下的寬度。這是 LinearLayout 最實用的特性:不需要寫死 pixel 數值就能做等分排列。
允許以「相對於父容器」或「相對於其他元件」的方式定位,能有效減少巢狀層數。
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >
<Button android:id="@+id/bbtn1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="中央"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
<Button android:id="@+id/bbtn2" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="左上角"
android:layout_above="@id/bbtn1" android:layout_toLeftOf="@id/bbtn1" />
<Button android:id="@+id/bbtn3" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="左下角"
android:layout_below="@id/bbtn1" android:layout_toLeftOf="@id/bbtn1" />
<Button android:id="@+id/bbtn4" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="右下"
android:layout_below="@id/bbtn1" android:layout_toRightOf="@id/bbtn1" />
<Button android:id="@+id/bbtn5" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="右上"
android:layout_above="@id/bbtn1" android:layout_toRightOf="@id/bbtn1" />
</RelativeLayout>
結果:

這裡有個實務上很重要的規則:被參考的元件必須先出現在配置檔中(或在程式中先建立),否則 @id/bbtn1 會找不到而導致定位失效。另外,RelativeLayout 的效能與巢狀深度及參考官索的複雜度有關,層級太深時配置時間會明顯變長。
以列與行構成網格,支援 columnSpan 與 rowSpan 做跨格合併,很適合做出計算機那類的鍵盤版面。
<GridLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:rowCount="5"
android:columnCount="4" >
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="7" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="8" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="9" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="÷" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="4" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="5" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="6" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="×" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="=" android:layout_rowSpan="2"
android:layout_gravity="fill" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="+" android:layout_columnSpan="2"
android:layout_gravity="fill" />
</GridLayout>
結果:

這裡的 layout_gravity="fill" 是必要的——單純設定 rowSpan 或 columnSpan 只會讓元件佔據更多格子,若不搭配 gravity,元件本身仍會維持原本的大小而不會被拉滿。
TableLayout 實際上是 LinearLayout 的擴充,每一列用 TableRow 表示。它的特色是可以針對「特定欄」設定伸縮行為。
<TableLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1"
android:shrinkColumns="2"
android:padding="3dip" >
<TableRow>
<TextView android:text="名稱" />
<TextView android:text="數值" />
</TableRow>
<TableRow>
<TextView android:text="很長的一段文字內容用來示範收縮行為" />
<Button android:text="確認" />
</TableRow>
</TableLayout>
結果:

stretchColumns 指定哪些欄可以伸展填滿剩餘空間,shrinkColumns 則指定哪些欄在空間不足時可以被壓縮。這兩個屬性是 TableLayout 相對於 GridLayout 的主要差異,也是它在處理「欄寬需要依內容調整」的表格時仍有價值的原因。
實務上的判斷可以很簡單:內容是清單就別用這些容器,直接上 ListView 或 RecyclerView;需要等分排列用 LinearLayout + weight;需要重疊或少量相對定位用 RelativeLayout(或 ConstraintLayout);規則的網格用 GridLayout;欄寬需依內容伸縮的表格用 TableLayout。
最後補充一點:現在新專案幾乎都會改用 ConstraintLayout。它能用扁平的一層結構表達原本需要多層巢狀才能做到的版面,對效能與維護性都有明顯好處,上面這幾種布局則在讀懂舊專案時仍然必要。
Android 初級教程:讓 PopupWindow 帶着動畫弹出
這一篇把動畫知識與彈出窗體結合起來,做出一個帶進場動畫的 PopupWindow。動畫基礎在前面幾篇已經詳細介紹過,這裡直接進入實踐。 一、PopupWindow 的基
Android 用 WebView 登入,再把 Session Cookie 交棒給 HttpClient
前陣子做了一個小功能:登入介面用 HTML 頁面實作,登入成功後把 cookie 取出來,交給 HttpClient 去跑後續的業務請求。這樣的好處是登入表單可以隨時改版
Android 中 ListView 結合 CheckBox 實作資料批量選擇(全選、反選、全不選)
在 App 開發中,經常會遇到這樣的需求:批量取消(刪除)List 中的資料。這就要求 ListView 支援批量選擇、全選、單選等功能,因此打造一個功能完善的 List
自定義組合控件 Titlebar 完整實踐:從自定義屬性到代碼布局
前言:我相信「天生我才必有用」這句話。每個人都有自己的作用,也許相對別人不明顯,也許只是為了取悅他人,但請別忘了,我們可以透過持續努力把價值提上來。 日常 Android