TableLayout和我们平时在网页上见到的Table有所不同,TableLayout没有边框的,它是由多个TableRow对象组成,每个TableRow可以有0个或多个单元格,每个单元格就是一个View。这些TableRow,单元格不能设置layout_width,宽度默认是fill_parent的,只有高度layout_height可以自定义,默认是wrap_content。

单元格可以为empty,并且通过android:layout_column可以设置index值实现跳开某些单元格。在TableRow之间,添加View,设置layout_height以及背景色,就可以实现一条间隔线。android:layout_span可以设置合并几个单元格:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:stretchColumns="1">
<TableRow>
<TextView android:text="用户名" android:textStyle="bold"
android:gravity="right" android:padding="3dip" />
<EditText android:id="@+id/username" android:text="用户名"
android:padding="3dip" android:scrollHorizontally="true" />
</TableRow>
<TableRow>
<TextView android:text="密码" android:textStyle="bold"
android:gravity="right" android:padding="3dip" />
<EditText android:id="@+id/password" android:text="密码"
android:password="true" android:padding="3dip"
android:scrollHorizontally="true" />
</TableRow>
<TableRow android:gravity="right">
<Button android:id="@+id/cancel" android:text="cancel" />
<Button android:id="@+id/login" android:text="login" />
</TableRow>
<!-- 间隔线 -->
<View android:layout_height="2dip" android:background="#F00" />
<View android:layout_height="5dip" />
<TableRow>
<TextView android:text="合并2个单元格" android:layout_span="2"
android:gravity="center_horizontal" android:background="#FFC0C0C0"
android:textColor="#f00" android:padding="3dip" />
</TableRow>
</TableLayout>



运行效果:


[img]http://dl.iteye.com/upload/attachment/515770/bb466197-be9f-34d7-a19c-679504df0db3.jpg[/img]