布局Layout管理
布局即是指Activity中組件的呈現方式,即組件大小、間距和對齊方式等。
Android提供了兩種創建布局的方式:
1.在XML配置文件中聲明(推薦)。
2.在程序中通過代碼直接實例化布局及其組件。
在Android中常見的布局方式:
線性布局(LinearLayout):按照垂直或者水平方向布局組件。
幀布局(FrameLayout):組件從屏幕的左上角坐標布局組件。
表格布局(TableLayout):按照行列方式布局組件。
相對布局(RelativeLayout):相對其他組件的布局方式。
絕對布局(AbsoluteLayout):按照絕對坐標來布局組件。(已廢)。
各種布局方式之間可以互相嵌套,只需要將布局理解成為一個容器控件即可,官方的說法叫View Group,見http://developer.android.com/guide/topics/ui/overview.html。
線性布局LinearLayout
線性布局是將子組件按照垂直或者水平方向來布局。
線性布局的方向由
android:orientation="vertical"或者"horizontal"來控制。
一般情況下都是在LinearLayout的開頭就設定方向和寬高,至於里面擺放的控件,就具體設定其控件的屬性。
幫助文檔:
http://developer.android.com/reference/android/widget/LinearLayout.html
里面有很多的屬性介紹。
比較常用的是:
android:gravity屬性,指定控件的基本位置,如設置TextView中的文字的位置。
android:background指定控件使用的背景色
android:padding指定控件的內邊距,設定一個值之后四個邊的內邊距都是這個值。
android:layout_weight屬性用來控制各個控件所占空間的權重。
例子:
自己新建一個布局文件,放在res\layout文件夾里(貌似文件名必須都是小寫字母。)
布局文件如下:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id = "@+id/firstText" android:text="@string/TextOne" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" > </TextView> <TextView android:id = "@+id/secondText" android:text="@string/TextTwo" android:gravity="center_vertical" android:background="@color/red" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="2" > </TextView> <TextView android:id = "@+id/thirdText" android:text="@string/TextThree" android:gravity="center" android:textColor="@color/blue" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="3" > </TextView> </LinearLayout>
運行結果如下:
幀布局FrameLayout
http://developer.android.com/reference/android/widget/FrameLayout.html
幀布局是從屏幕的左上角坐標(0,0)開始布局,多個組件層疊排序,后面的組件覆蓋前面的組件。
幀布局中沒有權重這一說。
把上文中的LinearLayout改為FrameLayout,
程序運行結果如下:
其中紅色是第二個TextView的背景色,而第三個TextView沒有背景色,直接覆蓋在上面了。
表格布局TableLayout
http://developer.android.com/reference/android/widget/TableLayout.html
表格布局以行、列表格的方式布局子組件。
TableLayout中使用TableRow來定義多行。
TableLayout中如果不用TableRow,則所有控件從上到下排列。
用了TableRow,每個TableRow中的控件構成多列。
如下圖,每一個TableRow中放兩個TextView:
屬性android:stretchColumns表示拉伸列,表示如果填充不滿時,拉伸該序號(序號從0開始)的列,填滿空間。
比如設置
android:stretchColumns="0"后,拉伸第一列。
如下圖:
android:collapseColumns表示隱藏指定的列。
表格布局的代碼:

<?xml version="1.0" encoding="UTF-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:stretchColumns="0" > <TableRow> <TextView android:id = "@+id/firstText" android:text="@string/TextOne" android:layout_width="fill_parent" android:layout_height="wrap_content" > </TextView> <TextView android:id = "@+id/secondText" android:text="@string/TextTwo" android:gravity="center_vertical" android:background="@color/red" android:layout_width="fill_parent" android:layout_height="wrap_content" > </TextView> </TableRow> <TableRow> <TextView android:id = "@+id/thirdText" android:text="@string/TextThree" android:gravity="center_vertical" android:textColor="@color/blue" android:layout_width="fill_parent" android:layout_height="wrap_content" > </TextView> <TextView android:id = "@+id/fourthText" android:text="@string/TextFour" android:gravity="center_vertical" android:background="@color/green" android:textColor="@color/blue" android:layout_width="fill_parent" android:layout_height="wrap_content" > </TextView> </TableRow> </TableLayout>
相對布局RelativeLayout
相對布局按照組件之間的相對位置來布局,如在某個組件的左右上下等。
http://developer.android.com/reference/android/widget/RelativeLayout.html
通過設置控件的屬性來設置控件的相對位置。
屬性可以分為四大類:
第一類:上下左右四個相對位置。
第二類:邊緣對齊的五個屬性(加上一個基線對齊)。
第三類:是否和父控件在上下左右邊緣對齊的四個屬性。
第四類:居中方式的三個屬性。
前兩類設置時設置指定控件的id,后兩類的值為true或者false。
絕對布局AbsoluteLayout
絕對布局通過指定子組件的確切XY坐標位置,該類已經過期,可以使用其他布局代替之。
資源
布局教程匯總:http://www.apkbus.com/android-50865-1-1.html
官方文檔教程:http://developer.android.com/guide/topics/ui/declaring-layout.html