android报表布局TableLayout
android表格布局TableLayout
TableLayout由所表格布局采用行列的形式来管理UI组件,TableLayout不需要明确声明包含多少行和列,而是通过添加TableRow,其他组件来控制表格的行列的。
向表格中添加TableRow和其他组件,该表格就增加一行,该组件占有一行。
表格的单元格常用三种属性:
Shrinkable:某列宽度可以被收缩,以保证适应父容器的宽度
Stretchable:某列宽度可以被拉伸,适应父容器的空间
Collapsed:该列的所有单元格会隐藏
Attribute Name | Related Method | Description |
android:collapseColumns | setColumnCollapsed(int,boolean) | The zero-based index of the columns to collapse. |
android:shrinkColumns | setShrinkAllColumns(boolean) | The zero-based index of the columns to shrink. |
android:stretchColumns | setStretchAllColumns(boolean) | The zero-based index of the columns to stretch. |
调用setColumnCollapsed(int,boolean)等方法的时候,多个列序号之间用逗号隔开
demo代码:
<?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"> <TableLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:shrinkColumns="1" android:stretchColumns="2"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第一个表格" /> </TableLayout> <TableLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:collapseColumns="2"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第二个表格" /> <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" /> </TableRow> </TableLayout> <TableLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:stretchColumns="0,1,2"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第三个表格" /> <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" /> </TableRow> </TableLayout> </LinearLayout>
效果: