表格布局的風格跟 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 >
界面效果如下: