表格布局(TableLayout)
前言
表格布局采用行和列的方式来管理UI组件,但不像Android 4.0 新增的网格布局,直接设置行列数
如果我们直接向TableLayout添加组件的话,那么这个组件将独占一行
如果要让多个组件在同一行的话,那么我们要添加一个TableRow的容器,用于存储这些组件
而这些组件的数目也决定了该行有多少列
列的宽度由该列中最宽的单元格决定
整个表格布局的宽度则取决于父容器的宽度(占满父容器本身)
三个常用的属性
android:collapseColumns:设置需要被隐藏的列的序号
android:shrinkColumns:设置允许被收缩的列的列序号
android:stretchColumns:设置运行被拉伸的列的列序号
注意:
!!!!!!!!序号是从0开始的,可以同时设置多个列,用逗号","分隔即可
这些属性要写在TableLayout中哦!
下面依次演示下每个属性设置以后的效果:
①collapseColumns(隐藏列)
步骤:在TableRow中先定义了5个按钮,接着在最外层的TableLayout中添加以下属性:
android:collapseColumns = "0,2"
隐藏第一和第三列
代码如下:
- <TableLayout
- android:id="@+id/TableLayout2"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:collapseColumns="0,2" >
- <TableRow
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" >
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="one" />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="two" />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="three" />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="four" />
- <Button
- android:layout_width="wrap_content"