編輯:中階開發
Android 提供了五種基礎布局容器,分別是 FrameLayout(框架布局)、LinearLayout(線性布局)、AbsoluteLayout(絕對布局)、RelativeLayout(相對布局)與 TableLayout(表格布局)。掌握它們各自的排版邏輯,是設計手機介面的第一步。以下逐一說明,並附上 XML 範例。
FrameLayout 是最簡單的布局物件,相當於螢幕上一塊空白的備用區域,你可以在其中放入單一物件,例如一張要顯示的圖片。它所有子元素都會固定在左上角,你無法為 FrameLayout 中的子元素指定位置;後加入的子元素會直接疊在前一個子元素之上,將其部分或全部遮住(除非後者是透明的)。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:text="button"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:text="textView"
android:textColor="#0000ff"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>
LinearLayout 會依照你設定的 orientation(垂直或水平)來排列所有子元素。垂直列表中每一行只放一個元素,水平列表則只有一行的高度(等於最高子元素的高度加上邊距)。它能維持子元素之間的間距與對齊方式(靠左、居中或靠右)。
LinearLayout 最強大的特色是支援 weight 權重屬性,允許子元素按比例瓜分螢幕剩餘空間,避免在大螢幕上一堆小物件擠成一團。指定 weight 後,剩餘空間會依比例分配,預設值為 0。舉例來說,三個文字方塊中若有兩個設為 weight=1,這兩個就會等比例放大填滿剩餘空間,第三個則維持原樣。
<?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="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2">
<TextView
android:text="Welcome to Mr Wei's blog"
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<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>
RelativeLayout 允許子元素透過 ID,指定自己相對於其他元素或父元素的位置。你可以讓某元素靠右對齊、位於另一元素上下方,或置於螢幕正中央。元素依定義順序排列,若第一個元素在螢幕中央,其他元素便以它為基準來定位。使用 XML 時,被參照的目標元素必須先定義。
<?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="fill_parent">
<TextView
android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Welcome to Mr Wei's blog:" />
<EditText
android:id="@+id/entry"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
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="10dip"
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>
TableLayout 會把子元素分配到列(row)與行(column)組成的表格中。它由多個 TableRow 構成,每個 TableRow 定義一列,每列可擁有零個或多個儲存格,每格對應一個 View。需要注意的是,TableLayout 不會顯示列、行或儲存格的邊框線;儲存格允許留空,但不能跨列,這點與 HTML 表格不同。
<?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="1">
<TableRow>
<TextView android:layout_column="1" android:text="Open..." />
<TextView android:text="Ctrl-O" android:gravity="right" />
</TableRow>
<TableRow>
<TextView android:layout_column="1" android:text="Save..." />
<TextView android:text="Ctrl-S" android:gravity="right" />
</TableRow>
<View android:layout_height="2dip" android:background="#FF909090" />
<TableRow>
<TextView android:text="X" />
<TextView android:text="Export..." />
<TextView android:text="Ctrl-E" android:gravity="right" />
</TableRow>
<TableRow>
<TextView android:layout_column="1" android:text="Quit"
android:padding="3dip" />
</TableRow>
</TableLayout>
其中 android:stretchColumns 用來指定哪些列可以拉伸填滿剩餘寬度,而那段高 2dip 的 View 則是畫面上的分隔線。
AbsoluteLayout 讓子元素指定精確的 x/y 座標(左上角為 (0,0),向右、向下移動時數值變大)。它沒有頁邊框,允許元素互相重疊。不過官方並不建議使用它,除非有正當理由,因為這種過於剛性的寫法往往無法在不同尺寸、解析度的裝置上良好呈現。
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText
android:text="Welcome to Mr Wei's blog"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:layout_x="250px"
android:layout_y="40px"
android:layout_width="70px"
android:layout_height="wrap_content"
android:text="Button" />
</AbsoluteLayout>
實務上,LinearLayout 與 RelativeLayout 是最常用、也最值得優先熟悉的兩種;FrameLayout 適合需要層疊效果的單純場景;TableLayout 適合表格化資料;而 AbsoluteLayout 則因缺乏自適應能力,如今已很少使用。理解每種布局的排版思維,才能在面對不同介面需求時做出合適的取捨。
Action Bar 使用方法 - Android 活動欄(一)
在 Android 3.0 中,除了我們重點講解的 Fragment 之外,Action Bar 同樣是一項重要內容。Action Bar 主要用來取代傳統的標題欄,對於
Android 特色開發:Google Map 與桌面組件入門(1)
本文節選自《Android 應用開發揭秘》的相關內容,重點放在 Android 平台中颇具特色的地圖與桌面組件開發。Android 除了基本的 UI 與資料管理之外,也提
Android 3.x 平板相容性設計:讓 1.5~2.3 的 App 跑在 Honeycomb 上
Android 3.x(Honeycomb)是為平板而生的一代,螢幕尺寸與解析度都遠高於早期手機,因此舊有應用要在平板上正確顯示,需要做一些資源與版面配置上的調整。以下說
Android 除錯工具 TraceView 圖解實測
Android SDK 自帶的效能分析工具 TraceView,前面在介紹 android.os.Debug 時已經提過。這篇就來實際跑一次,看看它長什麼樣子。 一、在