表格布局的风格跟 HTML 中的表格比较接近,只是所采用的标签不同。
□<TableLayout > 是顶级元素,采用的是表格布局
□ <TableRow> 定义一个行
□ <TextView > 定义一个单元格的内容
示例main.xml布局文件内容如下:
- <? 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 ="0,1,2,3"
- >
- < TableRow >
- < TextView
- android:text = "@string/name"
- android:gravity = "center"
- android:padding = "3dip" />
- < TextView
- android:text = "@string/gender"
- android:gravity = "center"
- android:padding = "3dip" />
- < TextView
- android:text = "@string/age"
- android:gravity = "center"
- android:padding = "3dip" />
- < TextView
- android:text = "@string/phonenum"
- android:gravity = "center"
- android:padding = "3dip" />
- </ TableRow >
- < TableRow >
- < TextView
- android:text = "@string/name1"
- android:gravity = "center"
- android:padding = "3dip" />
- < TextView
- android:text = "@string/gender1"
- android:gravity = "center"
- android:padding = "3dip" />
- < TextView
- android:text = "@string/age1"
- android:gravity = "center"
- android:padding = "3dip" />
- < TextView
- android:text ="@string/phonenum1"
- android:gravity = "center"
- android:padding = "3dip" />
- </ TableRow >
- < TableRow >
- < TextView
- android:text = "@string/name2"
- android:gravity = "center"
- android:padding = "3dip" />
- < TextView
- android:text = "@string/gender1"
- android:gravity = "center"
- android:padding = "3dip" />
- < TextView
- android:text = "@string/age2"
- android:gravity = "center"
- android:padding = "3dip" />
- < TextView
- android:text ="@string/phonenum2"
- android:gravity = "center"
- android:padding = "3dip" />
- </ TableRow >
- </ TableLayout >
□ android:stretchColumns="0,1,2,3"
该属性指定每行都由第“ 0 、 1 、 2 、 3 ”列占满空白空间。
□ gravity 指定文字对齐方式,案例都设为居中对齐。
□ padding 指定视图与视图内容间的空隙,单位为像素。
对应的 strings.xml 文件内容如下:
- <? xml version = "1.0"encoding = "utf-8" ?>
- < resources >
- < string name = "name" > 姓名 </string >
- < string name = "gender" > 性别 </string >
- < string name = "age" > 年龄 </string >
- < string name = "phonenum"> 电话 </ string >
- < string name = "gender1" >男 </ string >
- < string name = "gender2" >女 </ string >
- < string name = "name1" > 张三 </string >
- < string name = "age1" > 25</ string >
- < string name = "phonenum1"> 1234567 </ string >
- < string name = "name2" > 李四 </string >
- < string name = "age2" > 24</ string >
- < string name = "phonenum2"> 7654321 </ string >
- </ resources >
界面效果如下: