<?xml version="1.0" encoding="utf-8"?>
<TableLayout android:id="@+id/tableLayout1" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1"
android:shrinkColumns ="1"> <!-- android:stretchColumns 第二列自动伸展-->
<TextView android:text="Please fill the form"
android:layout_span="2"
android:gravity="center"/><!--元素内容居中对齐-->
<TableRow>
<TextView android:text="用户名:"
android:gravity="right"/>
<EditText android:id="@+id/username" />
</TableRow>
<TableRow >
<TextView android:text="登录密码"/>
<EditText android:id= "@+id/password" android:password="true"/>
</TableRow>
<TableRow >
<TextView android:layout_height="2dip"
android:background="#FF909090"
android:layout_column = "1"/><!-- 指定显示的列数,这儿是第二列 -->
</TableRow>
<TableRow >
<Button android:id="@+id/cancel" android:text="取消"/>
<Button android:id="@+id/ok" android:text="登录"
android:padding="4dip"/><!-- 元素内容与边界之间保留4dip的距离 -->
</TableRow>
</TableLayout>
效果如下:
其中 android:stretchColumns=”1″ 作用是让第二列可以扩展到所有可用空间;下面我们讲一下TableLayout几个重要的属性:
collapseColumns – 设置隐藏那些列,列ID从0开始,多个列的话用”,”分隔
stretchColumns – 设置自动伸展那些列,列ID从0开始,多个列的话用”,”分隔
shrinkColumns -设置自动收缩那些列,列ID从0开始,多个列的话用”,”分隔
可以用”*”来表示所有列,同一列可以同时设置为shrinkable和stretchable。
TableRow本身也是一个Layout,里面的元素会安装水平方向依次排列,如果TableRow的父元素不是TableLayout的话,那么他会表现的像一个LinearLayout。