Android开发布局二_表格布局(TableLayout)

     表格布局即,tableLayout,表格布局通过行、列的形式来管理UI组件,TablelLayout并不需要明确地声明包含多少行、多少列,而是通过TableRow,以及其他组件来控制表格的行数和列数,

    TableRow也是容器,因此可以向TableRow里面添加其他组件,没添加一个组件该表格就增加一列。

    如果想TableLayout里面添加组件,那么该组件就直接占用一行。

  在表格布局中,列的宽度由该列中最宽的单元格决定,整个表格布局的宽度取决于父容器的宽度(默认是占满父容器本身)。

TableLayout继承了LinearLayout,因此他完全可以支持LinearLayout所支持的全部XML属性,除此之外TableLayout还支持以下属性:

    XML属性                                       相关用法                                                    说明

1. andriod:collapseColumns           setColumnsCollapsed(int ,boolean)       设置需要隐藏的列的序列号,多个用逗号隔开

2.android:shrinkColumns              setShrinkAllColumns(boolean)                 设置被收缩的列的序列号,多个用逗号隔开

3.android:stretchColimns             setSretchAllColumnds(boolean)               设置允许被拉伸的列的序列号,多个用逗号隔开

 

  代码如下所示:

View Code
 1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:orientation="vertical"
4 android:layout_width="fill_parent"
5 android:layout_height="fill_parent"
6 >
7 <!-- 定义第一个表格布局,指定第二列允许收缩,第三列允许拉伸 -->
8 <TableLayout
9 android:id="@+id/tablelayout1"
10 android:layout_width="fill_parent"
11 android:layout_height="wrap_content"
12 android:shrinkColumns="1"
13 android:stretchColumns="2"
14 >
15 <!-- 直接添加一个button,他自己会占用一行 -->
16 <Button
17 android:layout_width="wrap_content"
18 android:layout_height="wrap_content"
19 android:text="独自占用一行的按钮"/>
20
21
22 <!-- 先添加一个tableRow,在添加三个button, 结果应该是三个button在这个tableRow(行)里面,即排列为一行 -->
23 <TableRow >
24 <Button
25 android:layout_width="wrap_content"
26 android:layout_height="wrap_content"
27 android:text="RBtn1"/>
28 <Button
29 android:layout_width="wrap_content"
30 android:layout_height="wrap_content"
31 android:text="RBtn2"/>
32 <Button
33 android:layout_width="wrap_content"
34 android:layout_height="wrap_content"
35 android:text="RBtn3"/>
36 </TableRow>
37 </TableLayout>
38 </LinearLayout>

上面代码展示了,在tableLayour中使用TableRow的效果,和不使用TableRow的效果,并第二列允许收缩,第三列允许拉伸。运行结果如下所示:

 

下面说明XML属性的隐藏使用方法,在TableLayout中添加一下代码,

 

android:collapseColumns="0"

代码如下所示:

 

注意:属性中设置列号的时候是从0,1,2,3 ....

         不是1,2,3....


 

posted on 2011-09-29 10:16  wangbokun  阅读(1801)  评论(0编辑  收藏  举报

导航