標籤:
表格版面配置有TableLayout所代表,TableLayout繼承了LinearLayout,因此他的本質依然是LinearLayout。
表格版面配置採用行、列的形式來進行管理,在使用的時候不需要聲明多少行、多少列,而是通過添加TableRow、其他組件來控製表格的行數和列數。
每次向TableLayout添加一個TableRow,該TableRow就是一個表格行,同時TableRow也是容器,可以在其中不斷的添加其他的組件,每添加一個子組件,該表格的列就增加一列
在表格版面配置管理器中,可以為儲存格設定如下三種行為方式
- Shrinkable:如果某一列被設定為Shrinkable,那麼該列的所有儲存格的寬度可以被收縮,以保證該表格能適應父容器的寬度
- Stretchable:如果某一列被設定為Stretchable,那麼該列的所有儲存格的寬度可以被展開,以保證組件能全部填滿表格空餘控制項
- Collapsed:如果某個列被設定為該屬性,那麼該列的所有儲存格都會被隱藏
因為TableLayout繼承了LinearLayout類,因此除了支援LinearLayout的全部屬性外還支援下面的三個屬性
XML屬性 |
方法 |
說明 |
android:collapseColumns |
setColumnCollapsed(int,boolean) |
設定需要被隱藏的列的序號,多個使用都逗號隔開 |
android:shrinkColumns |
setShrinkAllColumns(boolean) |
設定允許被收縮的列的序號,多個使用逗號隔開 |
android:stretchColumns |
setStretchAllColumns(boolean) |
設定允許被展開的列的序號,多個使用逗號隔開 |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" tools:context="com.example.tablelayout.MainActivity" > <!-- 第一個TableLayout,指定二列允許收縮,三列允許展開 --> <TableLayout android:id="@+id/tableLayout_one" android:layout_width="fill_parent" android:layout_height="wrap_content" android:shrinkColumns="1" android:stretchColumns="3"> <Button android:id="@+id/ok1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="獨自一行的按鈕"/> <TableRow> <Button android:id="@+id/ok2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="普通按鈕"/> <Button android:id="@+id/ok3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="收縮按鈕"/> <Button android:id="@+id/ok4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="展開按鈕"/> </TableRow> </TableLayout> <!-- 第二個TableLayout,指定二列允隱藏--> <TableLayout android:id="@+id/tableLayout_two" android:layout_width="fill_parent" android:layout_height="wrap_content" android:collapseColumns="2"> <Button android:id="@+id/ok5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="獨自一行的按鈕"/> <TableRow> <Button android:id="@+id/ok6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="普通按鈕"/> <Button android:id="@+id/ok7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="收縮按鈕"/> <Button android:id="@+id/ok8" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="隱藏按鈕"/> </TableRow> </TableLayout> <!-- 第三個TableLayout,指定二列允許展開,三列允許展開 --> <TableLayout android:id="@+id/tableLayout_three" android:layout_width="fill_parent" android:layout_height="wrap_content" android:shrinkColumns="1,2"> <Button android:id="@+id/ok9" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="獨自一行的按鈕"/> <TableRow> <Button android:id="@+id/ok10" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="普通按鈕"/> <Button android:id="@+id/ok11" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="展開按鈕"/> <Button android:id="@+id/ok12" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="展開按鈕"/> </TableRow> </TableLayout></LinearLayout>
Android布局管理器(表格版面配置)