表格布局TableLayout 類以行和列的形式管理控件,每行為一個TableRow 對象,也可以為一個View對象,當(dāng)為View 對象時,該View 對象將跨越該行的所有列。在TableRow 中可以添加子控件,每添加一個子控件為一列。
TableLayout 布局中并不會為每一行、每一列或每個單元格繪制邊框,每一行可以有0 或多個單元格,每個單元格為一個View 對象。TableLayout 中可以有空的單元格,單元格也可以像HTML 中那樣跨越多個列。圖-1是表格布局的示意圖。

圖-1 表格布局示意圖
在表格布局中,一個列的寬度由該列中寬的那個單元格指定,而表格的寬度是由父容器指定的。在TableLayout 中,可以為列設(shè)置3種屬性。
`Shrinkable,如果一個列被標(biāo)識為shrinkable,則該列的寬度可以進行收縮,以使表格能夠適應(yīng)其父容器的大小。
`Stretchable,如果一個列被標(biāo)識為stretchable,則該列的寬度可以進行拉伸,以填滿表格中空閑的空間。
`Collapsed,如果一個列被標(biāo)識為collapsed,則該列將會被隱藏。
注意:一個列可以同時具有Shrinkable 和Stretchable 屬性,在這種情況下,該列的寬度將任意拉伸或收縮以適應(yīng)父容器。
TableLayout 繼承自LinearLayout 類,除了繼承來自父類的屬性和方法,TableLayout 類中還包含表格布局所特有的屬性和方法。這些屬性和方法說明如表1-1所示。
表1-1 TableLayout 類常用屬性及對應(yīng)方法說明
屬性名稱 |
對應(yīng)方法 |
描述 |
android:collapseColumns |
setColumnCollapsed(int,boolean) |
設(shè)置指定列號的列為Collapsed,列號從0 |
android:shrinkColumns |
setShrinkAllColumns(boolean) |
設(shè)置指定列號的列為Shrinkable,列號從0 |
android:stretchColumns |
setStretchAllColumns(boolean) |
設(shè)置指定列號的列為Stretchable,列號從0 |
以下我們用一個表格布局的例子來加深對表格布局的理解,首先,建立表格布局要注意以下幾點:
(1)向界面中添加一個表格布局,無須修改布局的屬性值。其中,ID屬性為TableLayout01,Layout width和Layout height屬性都為wrap_content。
(2)向TableLayout01中添加兩個TableRow。TableRow代表一個單獨的行,每行被劃分為幾個小的單元,單元中可以添加一個界面控件。其中,ID屬性分別為TableRow01和TableRow02,Layout width和Layout height屬性都為wrap_content。
(3)通過Outline,向TableRow01中添加TextView和EditText;向TableRow02中添加兩個Button。

圖-2 向TableRow01中添加TextView和EditText
參考表1-2設(shè)置TableRow中4個界面控件的屬性值。
表1-2 表格布局控件屬性
編號 |
類型 |
屬性 |
值 |
1 |
TextView |
id |
text |
gravity |
padding |
Layout_width |
|
@+id/label |
用戶名: |
right |
3dip |
160dip |
|
2 |
EditText |
id |
text |
padding |
Layout_width |
|
@+id/entry |
[null] |
3dip |
160dip |
|
3 |
Button |
|
|
3 |
Button |
|
|
(4)main.xml完整代碼如代碼清單1所示。
代碼清單1 main.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout android:id="@+id/TableLayout01"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TableRow android:id="@+id/TableRow01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:id="@+id/label"
android:layout_height="wrap_content"
android:layout_width="160dip"
android:gravity="right"
android:text="用戶名:"
android:padding="3dip" >
</TextView>
<EditText android:id="@+id/entry"
android:layout_height="wrap_content"
android:layout_width="160dip"
android:padding="3dip" >
</EditText>
</TableRow>
<TableRow android:id="@+id/TableRow02"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button android:id="@+id/ok"
android:layout_height="wrap_content"
android:padding="3dip"
android:text="確認(rèn)">
</Button>
<Button android:id="@+id/Button02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:text="取消">
</Button>
</TableRow>
</TableLayout>
代碼中,第3行代碼使用了<TableLayout>標(biāo)簽聲明表格布局;第7行和第23行代碼聲明了兩個TableRow元素;第12行設(shè)定寬度屬性android:layout_width:160dip;第13行設(shè)定屬性android:gravity,指定文字為右對齊;第15行使用屬性android:padding,聲明TextView元素與其他元素的間隔距離為3dip。
(5)表格布局運行效果如圖-3所示。

圖-3 表格布局運行圖