Android应用开发之TableLayout (表格布局)+信息列表案例


表格布局的风格跟 HTML 中的表格比较接近,只是所采用的标签不同。

□<TableLayout > 是顶级元素,采用的是表格布局

□ <TableRow> 定义一个行

□ <TextView > 定义一个单元格的内容

示例main.xml布局文件内容如下:

  1. <? xml version = "1.0"encoding = "utf-8" ?>  
  2. < TableLayout  
  3. xmlns:Android = "http://schemas.android.com/apk/res/android  
  4. android:layout_width ="fill_parent"  
  5. android:layout_height ="fill_parent"  
  6. android:stretchColumns ="0,1,2,3"  
  7. >   
  8. < TableRow >  
  9. < TextView  
  10. android:text = "@string/name"  
  11. android:gravity = "center"  
  12. android:padding = "3dip" />  
  13. < TextView  
  14. android:text = "@string/gender"  
  15. android:gravity = "center"  
  16. android:padding = "3dip" />  
  17. < TextView  
  18. android:text = "@string/age"  
  19. android:gravity = "center"  
  20. android:padding = "3dip" />  
  21. < TextView  
  22. android:text = "@string/phonenum"  
  23. android:gravity = "center"  
  24. android:padding = "3dip" />  
  25. </ TableRow >  
  26. < TableRow >  
  27. < TextView  
  28. android:text = "@string/name1"  
  29. android:gravity = "center"  
  30. android:padding = "3dip" />  
  31. < TextView  
  32. android:text = "@string/gender1"  
  33. android:gravity = "center"  
  34. android:padding = "3dip" />  
  35. < TextView  
  36. android:text = "@string/age1"  
  37. android:gravity = "center"  
  38. android:padding = "3dip" />  
  39. < TextView  
  40. android:text ="@string/phonenum1"  
  41. android:gravity = "center"  
  42. android:padding = "3dip" />  
  43. </ TableRow >  
  44. < TableRow >  
  45. < TextView  
  46. android:text = "@string/name2"  
  47. android:gravity = "center"  
  48. android:padding = "3dip" />  
  49. < TextView  
  50. android:text = "@string/gender1"  
  51. android:gravity = "center"  
  52. android:padding = "3dip" />  
  53. < TextView  
  54. android:text = "@string/age2"  
  55. android:gravity = "center"  
  56. android:padding = "3dip" />  
  57. < TextView  
  58. android:text ="@string/phonenum2"  
  59. android:gravity = "center"  
  60. android:padding = "3dip" />  
  61. </ TableRow >  
  62. </ TableLayout >  

□ android:stretchColumns="0,1,2,3"

该属性指定每行都由第“ 0 、 1 、 2 、 3 ”列占满空白空间。

□ gravity 指定文字对齐方式,案例都设为居中对齐。

□ padding 指定视图与视图内容间的空隙,单位为像素。

对应的 strings.xml 文件内容如下:

  1. <? xml version = "1.0"encoding = "utf-8" ?>  
  2. < resources >  
  3. < string name = "name" > 姓名 </string >  
  4. < string name = "gender" > 性别 </string >  
  5. < string name = "age" > 年龄 </string >  
  6. < string name = "phonenum"> 电话 </ string >  
  7. < string name = "gender1" >男 </ string >  
  8. < string name = "gender2" >女 </ string >  
  9. < string name = "name1" > 张三 </string >  
  10. < string name = "age1" > 25</ string >  
  11. < string name = "phonenum1"> 1234567 </ string >  
  12. < string name = "name2" > 李四 </string >  
  13. < string name = "age2" > 24</ string >  
  14. < string name = "phonenum2"> 7654321 </ string >  
  15. </ resources >  

界面效果如下:

相关内容