歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android應用開發之TableLayout (表格布局)+信息列表案例

Android應用開發之TableLayout (表格布局)+信息列表案例

日期:2017/3/1 10:49:23   编辑:Linux編程

表格布局的風格跟 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 >

界面效果如下:

Copyright © Linux教程網 All Rights Reserved