編輯:中階開發
布局決定了 Activity 介面上各個元件的排列方式。本文以實作角度出發,重點說明 LinearLayout 的嵌套與權重分配、TableLayout 的表格排版,以及 RelativeLayout 的相對定位屬性,並附上可直接套用的 XML 範例。
在動手排版之前,先熟悉幾個會反覆出現的屬性:
android:id:為元件指定唯一 ID。android:text:指定元件顯示的文字,建議盡量引用 strings.xml 中的字串資源。android:gravity:指定元件內容的對齊位置,例如居中、靠右等。android:textSize:指定字體大小。android:background:指定背景色,採 RGB 命名法。android:layout_width / android:layout_height:指定元件寬與高。android:padding*:指定內邊距,即內容與邊框之間的距離。android:singleLine:設為 true 時,內容會在同一行顯示。LinearLayout 顧名思義是以「一條線」的方式排列元件,可分為水平(horizontal)與垂直(vertical)兩型。它最大的特色是 android:layout_weight 權重屬性:把父容器的空間依比例分配給子元件。例如兩個元件都設 layout_weight="1",就等於把空間分成兩份、各占一份。
由於 LinearLayout 可以自由嵌套,只要靈活搭配水平與垂直方向,就能組出相當複雜的版面。以下範例展示一個垂直的外層容器,內含一個水平的彩色橫列與一個垂直的文字清單:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<TextView android:text="red"
android:gravity="center_horizontal"
android:background="#aa0000"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1" />
<TextView android:text="green"
android:gravity="center_horizontal"
android:background="#00aa00"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1" />
<TextView android:text="blue"
android:gravity="center_horizontal"
android:background="#0000aa"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<TextView android:text="row one"
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView android:text="row two"
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
TableLayout 以表格形式排版,透過 android:stretchColumns 指定哪些列可以拉伸填滿剩餘寬度(索引從 0 開始)。每一列以 TableRow 表示,列中的每個子元件即為一個儲存格:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="0">
<TableRow>
<TextView android:text="@string/row1_column1"
android:background="#aa0000" android:padding="3dip" />
<TextView android:text="@string/row1_column2"
android:gravity="center_horizontal"
android:background="#00aa00" android:padding="3dip" />
<TextView android:text="@string/row1_column3"
android:gravity="right"
android:background="#0000aa" android:padding="3dip" />
</TableRow>
<TableRow>
<TextView android:text="@string/row2_column1"
android:padding="3dip" />
<TextView android:text="@string/row2_column2"
android:gravity="right" android:padding="3dip" />
</TableRow>
</TableLayout>
布局容器之間可以互相嵌套。例如下方範例在外層用 LinearLayout 切分出上下兩塊(各占 weight=1),下半部再嵌入一個 TableLayout,就能在同一個畫面裡同時運用兩種排版邏輯,達到更細緻的版面安排。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<TextView android:text="red"
android:gravity="center_horizontal"
android:background="#aa0000"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1" />
<TextView android:text="green"
android:gravity="center_horizontal"
android:background="#00aa00"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<TableLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="0">
<TableRow>
<TextView android:text="@string/row1_column1" android:padding="3dip" />
<TextView android:text="@string/row1_column2"
android:gravity="right" android:padding="3dip" />
</TableRow>
</TableLayout>
</LinearLayout>
</LinearLayout>
RelativeLayout 以元件之間的相對方位來排版,透過一組 layout_ 屬性描述位置關係。常用屬性可分為兩類。
第一類是相對於「其他元件」:
android:layout_above:將本元件底部置於指定 ID 元件之上。android:layout_below:將本元件頂部置於指定 ID 元件之下。android:layout_toLeftOf:將本元件右緣與指定 ID 元件的左緣對齊。android:layout_toRightOf:將本元件左緣與指定 ID 元件的右緣對齊。android:layout_alignTop / android:layout_alignBottom:將本元件的頂部/底部與指定 ID 元件對齊。android:layout_alignLeft / android:layout_alignRight:將本元件的左緣/右緣與指定 ID 元件對齊。android:layout_alignBaseline:讓本元件與指定 ID 元件的基線對齊。第二類是相對於「父容器」:
android:layout_alignParentLeft / android:layout_alignParentRight:與父容器的左/右緣對齊。android:layout_alignParentTop / android:layout_alignParentBottom:與父容器的頂部/底部對齊。android:layout_centerHorizontal / android:layout_centerVertical:置於父容器水平/垂直方向的中央。android:layout_centerInParent:置於父容器正中央。以下範例做出一個典型的「輸入框 + OK / Cancel 按鈕」版面:文字標籤在上,輸入框緊接其下,OK 按鈕靠右對齊並位於輸入框下方,Cancel 按鈕則排在 OK 左側、頂部對齊。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10px">
<TextView android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Type here:" />
<EditText android:id="@+id/entry"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background"
android:layout_below="@id/label" />
<Button android:id="@+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/entry"
android:layout_alignParentRight="true"
android:layout_marginLeft="10px"
android:text="OK" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/ok"
android:layout_alignTop="@id/ok"
android:text="Cancel" />
</RelativeLayout>
LinearLayout 靠嵌套與 weight 靈活切分畫面,TableLayout 適合表格化資料,RelativeLayout 則以相對關係精準定位。三者若能搭配使用,幾乎可以應付絕大多數的介面排版需求。建議動手改一改範例中的屬性與權重,觀察畫面變化,對布局的理解會更深刻。
混合式應用程式模型打造 WebSphere Commerce 行動 App(下)
接續上篇,本文說明 iPhone 版本的四個進階能力,並完整介紹 Android 版本的實作方式。 可翻頁的產品詳情畫面 為了讓使用者能左右滑動瀏覽產品,我們自訂了一個
用 AIDL 打通 Service 與 Activity:讓 Service 主動回呼 Activity
AIDL 一直看起來有點玄,實際動手之後才發現思路其實很單純:Service 想呼叫 Activity 的方法,靠的是一組「反向的 Binder」。Activity 把自
Android 自定義按鈕:背景、9-patch 與異形按鈕實作
在 Android 應用開發中,系統預設的 Button 樣式往往較為制式。若想提升產品的視覺質感與操作體驗,通常需要自定義按鈕背景、形狀與狀態變化。本文整理三種常見做法
Android 3.0 Honeycomb 底層細節:硬體加速、Renderscript 與平板 UI
Honeycomb:Android 平板時代的起點 Android 3.0,代號 Honeycomb,是 Android 第一個主要針對平板設計的版本,也是首個導入硬體加