1、 TableLayout繼承了 LinearLayout,因此它的本質依然是線性布局管理器。每次向TableLayout中添加一個TableRow,該TableRow就是一個表格行,TableRow也是容器,因此它也可以不斷地添加其他組件,每添加一個子組件該表格就增加一列。如果直接向TableLayout中添加組件,那么這個組件將直接占用一行。
2、表格三種屬性
-
Shrinkable:如果某個列被設為Shrinkable,那么該列的所有單元格的寬度可以被收縮,以保證該表格能適應父容器的寬度。
-
Stretchable:如果某個列被設為Stretchable,那么該列的所有單元格的寬度可以被拉伸,以保證組件能完全填滿表格空余空間。
-
Collapsed:如果某個列被設為Collapsed,那么該列的所有單元格會被隱藏。
3、代碼示例
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".Main3Activity"> <!-- 定義第一個表格布局,指定第2列允許收縮,第3列允許拉伸 --> <TableLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:shrinkColumns="1" android:stretchColumns="2"> <!--單獨的按鈕 放在TableLayout里面占據一行--> <Button android:id="@+id/button14" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Button" /> <TableRow android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1按鈕" /> <Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1收縮" /> <Button android:id="@+id/button5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1拉伸" /> </TableRow> </TableLayout> <!-- 定義第2個表格布局,指定第2列隱藏--> <TableLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:collapseColumns="1"> <TableRow android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/button6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2后一個被隱藏" /> <Button android:id="@+id/button7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="被隱藏按鈕" /> <Button android:id="@+id/button8" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2前一個被隱藏" /> </TableRow> </TableLayout> <!--定義第3個表格布局,指定第2列和第3列可以被拉伸--> <TableLayout android:layout_width="match_parent" android:layout_height="match_parent" android:stretchColumns="1,2"> <TableRow android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/button9" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="3按鈕" /> <Button android:id="@+id/button10" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="3拉伸" /> <Button android:id="@+id/button11" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="3拉伸" /> </TableRow> <TableRow android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/button12" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="3按鈕" /> <Button android:id="@+id/button13" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="3拉伸" /> </TableRow> </TableLayout> </LinearLayout>
4、運行結果
5、注意事項
TableRow不需要設置寬度layout_width和高度layoutJieight,其寬度一定是match_parent,即自動填充父容器,高度一定為wrap_content,即根據內容改變高度。但對於TableRow中的其他控件來說,是可以設置寬度和高度的,但必其須是 wrap_content 或者 fill_parent。