編輯:Android 開發實例
Android 開發中最常打交道的三個容器是 LinearLayout、RelativeLayout 和 TableLayout。它們的排列邏輯各異,選對容器往往比堆疊嵌套更能簡化布局。
子組件或子容器按水平或垂直方向排成一條線。常用的五個屬性:
android:orientation:取值 horizontal 為水平排列,vertical 為垂直排列。android:layout_width 與 android:layout_height:每個子元素都必須聲明。android:layout_weight:按權重分配剩餘空間,控制各組件佔用的比例。android:layout_gravity:控制子組件在排列方向上的對齊方式。android:padding:內邊距,讓內容與邊緣保持距離。一個簡單的示例:
<?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" > <RadioGroup android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5px"> <RadioButton android:text="RadioButton1" /> <RadioButton android:text="RadioButton2" /> </RadioGroup> <RadioGroup android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="5px"> <RadioButton android:text="RadioButton1" /> <RadioButton android:text="RadioButton2" /> <RadioButton android:text="RadioButton3" /> </RadioGroup> </LinearLayout>
子組件的位置由「它與容器的關係」以及「它與其他組件的關係」共同決定,適合製作表單一類貼齊式界面。
組件相對於容器:android:layout_alignParentTop、layout_alignParentBottom、layout_alignParentLeft、layout_alignParentRight、layout_centerHorizontal、layout_centerVertical、layout_centerInParent。
組件相對於組件:android:layout_above、layout_below、layout_toLeftOf、layout_toRightOf 等。使用這一類屬性時,被引用的控件必須先定義 android: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="wrap_content" android:padding="5px"> <TextView android:id="@+id/label" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="URL:" android:paddingTop="15px"/> <EditText android:id="@+id/entry" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_toRightOf="@id/label" android:layout_alignBaseline="@id/label"/> <Button android:id="@+id/ok" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/entry" android:layout_alignRight="@id/entry" android:text="OK" /> <Button android:id="@+id/cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toLeftOf="@id/ok" android:layout_alignTop="@id/ok" android:text="Cancel" /> </RelativeLayout>
把子元素放進行與列構成的網格中。每一行用 <TableRow> 標識,android:stretchColumns 指定哪些列可以拉伸以填滿剩餘寬度,android:layout_column 指定控件所在的列序號,android:layout_span 則用於合併多列。
<?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:text="URL:" /> <EditText android:id="@+id/entry" android:layout_span="3"/> </TableRow> <View android:layout_height="2px" android:background="#0000FF" /> <TableRow> <Button android:id="@+id/cancel" android:layout_column="2" android:text="Cancel" /> <Button android:id="@+id/ok" android:text="OK" /> </TableRow> </TableLayout>
實踐中一個常見建議是:盡量減少嵌套層數。多層 LinearLayout 疊套會明顯增加測量與布局的開銷,能一層 RelativeLayout 解決的,就不必疊三層。
Android 應用本地化多語系實作教學
一款 Android App 很可能會在不同國家、不同地區的裝置上被使用。為了讓應用更貼近使用者,App 應該依據裝置所在的語言環境,自動調整文字、數字、檔案等內容的呈現
用 MediaPlayer 打造快轉、倒退都行的播放器
Android 播放音訊與影片的方式有好幾種,MediaPlayer 是最容易上手的一個。它位於 android.media 套件,除了本機檔案與 res/raw 資源,
Android ApiDemo 系列四:自訂 Dialog 主題
在 Android 中,Activity 的外觀可以透過 Theme 調整。ApiDemo 的 Custom Dialog 範例將一個 Activity 套用自訂主題,使
Android RSS閱讀器實作(二):用 SAX 解析 XML 與建立資料物件
要在 Android 上讀取 RSS,第一個要解決的問題是:如何解析 XML。RSS 本身就是 XML,因此只要掌握 XML 解析,就能把網路資料轉成應用程式可用的物件。