編輯:開發入門
Android 的版面系統由幾個基礎 Layout 組成,理解各自的特性與適用時機,是寫出好界面的第一步。以下逐一說明。
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 -->
<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 會依你指定的 vertical 或 horizontal 屬性來排列所有子元素。子元素被依序堆疊,因此垂直列表的每一列只有一個元素(不論它多寬),而水平列表只有一個列高(等於最高子元素的高度加上邊框)。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>
AbsoluteLayout 讓子元素指定精確的 x/y 座標來定位,(0, 0) 為左上角,往右下移動時座標值變大。它沒有頁邊框,允許元素互相重疊。
官方並不鼓勵使用 AbsoluteLayout,除非你有正當理由。因為它把界面寫得過於剛性,換到不同解析度或不同尺寸的裝置上很可能就跑歪了。
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="https://schemas.android.com/apk/res/android"
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>
RelativeLayout 允許子元素指定相對於其他元素或父元素的位置(透過 ID 參照)。因此你可以把兩個元素排成靠右對齊、上下相鄰,或置於畫面中央。
要特別注意的是它的排序依賴性:元素是按順序處理的,如果第一個元素置於畫面中央,那麼相對於它的其他元素就會以中央為基準來定位。因此在用 XML 撰寫時,被參照的元件必須先宣告,否則會找不到 ID。
<?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 把子元素分配到列與行。它由多個 TableRow 組成,每個 TableRow 定義一列(事實上你也可以直接放入其他子物件)。
幾個容易搞混的特性:TableLayout 容器不會顯示 row、column 或 cell 的邊框線;每一列擁有 0 個或多個 cell,每個 cell 容納一個 View 物件;表格允許儲存格為空;而且儲存格不能跨列,這點和 HTML 的 table 不同。若要撐開某幾欄的寬度,可以透過 android:stretchColumns 指定。
<?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>
<View android:layout_height="2dip" android:background="#FF909090" />
<TableRow>
<TextView android:layout_column="1" android:text="Quit"
android:padding="3dip" />
</TableRow>
</TableLayout>
其中那兩個 View 就是畫面上的分隔線——一個高度 2dip、灰色背景的空 View,這種小技巧在實作清單型界面時很好用。
實務上的建議是:預設用 LinearLayout 處理線性排列、RelativeLayout 處理需要相對定位的複合畫面,FrameLayout 用在只需要堆疊的單純場景(例如自訂 Dialog 或浮層),TableLayout 用在本身就是表格語義的資料呈現。AbsoluteLayout 則能避就避。這幾種 Layout 也可以任意巢狀組合,靈活搭配才能構出複雜又maintainable的界面。
Android 感測器開發指南:從方向與加速度到環境監控
讓 Android 成為感測與監控平台 Android 不只是手機作業系統,也是一個能接觸硬體感測資料的開發平台。對 Java 開發者而言,只要透過 Android SD
Android 語音識別入門:用 RecognizerIntent 把語音轉成文字
語音識別借助雲端運算,把使用者說出口的內容轉成文字,語音控制等應用都建立在這個基礎上。好消息是,Android 內建了一組 Intent API,讓 Google 的語音
用 Eclipse 開發 Android 應用程式(四):內容提供者與 Google Maps 實戰
打造內容提供者與 Google Maps 應用程式 你已經看過一個完整的應用程式範例,現在簡單談談更複雜的一款應用。 內容提供者與 Google Maps 本教學討論的第
Android學習之Android的智能指針
在Android源代碼中,我們經常會看到形如sp<xxx>、wp<xxx>這樣的類型定義,這其實就是Android中的智能指針。智能指針是C++中