1、特點
公共類TableLayout擴展LinearLayout容器不顯示其行、列或單元格的邊框線。每一行有零個或多個單元格;每個單元格可以包含一個視圖對象該表的列數與包含最多單元格的行的列數相同。一個表可以保留空單元格。單元格可以跨列,就像在HTML中一樣。
列的寬度由該列中單元格最寬的行定義。但是,TableLayout可以通過調用setColumnShrinkable()或由其父容器定義,將某些列指定為可收縮或可拉伸的。重要的是要記住,既可以收縮,也可以拉伸。在這種情況下,列將更改其大小,以始終使用可用空間,但永遠不會超過。最后,可以通過調用setcolumnclapsedo隱藏列。
TableLayout的子級不能指定布局寬度屬性。寬度始終匹配。但是,布局右心房可以由子節點定義;默認值為ViewGroup。包裝內容。如果孩子是一個表格行,那么高度總是ViewGroup。布局巴拉那。包裝內容。
在代碼和XML中,單元格必須按列的遞增順序添加到行中。列號是基於零的。如果不為子cel指定列號,它將自動遞增到下一個可用列。如果跳過列號,則該列號將被視為該行中的空單元格。有關使用XML創建表的示例,請參閱ApiDemos中的TableLayout示例。
盡管TableLayoutis的典型子類是table row,但實際上您可以將任何視圖子類用作TableLayout的直接子類該視圖將顯示為跨所有表列的單行。
(1)有一系列的tableRow對象組成,不顯示邊框線,可以標記為擴展或拉伸,總的寬度由父容器定義
(2)表格是不規則的
(3)某一列或幾列,可以設置成拉伸、收縮或隱藏
第一列拉伸:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" android:stretchColumns="0" > <TableRow> <Button android:text="button1"/> <Button android:text="button2"/> <Button android:text="button3"/> </TableRow> </TableLayout>
第一列拉伸,第二列收縮:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" android:stretchColumns="0" android:shrinkColumns="1"> <TableRow> <Button android:text="button1"/> <Button android:text="button2"/> <Button android:text="button3"/> </TableRow> </TableLayout>
隱藏第二列:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" android:collapseColumns="1" > <TableRow> <Button android:text="button1"/> <Button android:text="button2"/> <Button android:text="button3"/> </TableRow> </TableLayout>
(4)子組件不能設置layout_width屬性,因為它的寬度不能由自己來決定,可以定以高度
(5)列號是從零開始的
(6)可以用某一個View組件直接作為表格的子組件,則該組件獨占一行
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:text="button3"/> </TableLayout>
2、TableLayout實現計算器界面
(1)核心代碼:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <TextView android:layout_height="160sp" android:text="12345"/> <TableRow> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="mc"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="m+"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="m-"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="mr"/> </TableRow> <TableRow> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="C"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="/"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="*"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="刪除"/> </TableRow> <TableRow> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="7"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="8"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="9"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="-"/> </TableRow> <TableRow> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="4"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="5"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="6"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="+"/> </TableRow> <TableRow> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="3"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=""/> </TableRow> <TableRow> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="%"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="."/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="="/> </TableRow> </TableLayout>
(2)顯示效果: