一、表格布局的常见属性

  1. 1.android:collapseColumns 设置需要被隐藏的列的序号,从0开始<br /> 2.android:stretchColumns 设置允许被拉伸的列的列序号,从0开始(有一定的剩余空间才能被拉伸)<br /> 3.android:shrinkColumns 设置允许被收缩的列的列序号,从0开始 (有一定的超出范围才能被收缩)

子控件设置属性

  1. 1.android:layout_column 显示在第几列<br /> 2.android:layout_span 横向跨几列

二、表格布局的实现

  1. 1.布局tablelayout.xml文件
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:collapseColumns="2"
  6. android:stretchColumns="1"
  7. android:layout_width="match_parent"
  8. android:layout_height="match_parent"
  9. >
  10. <TableRow>
  11. <Button
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:layout_column="3"
  15. android:layout_span="2"
  16. android:text="第1个"
  17. />
  18. <Button
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"
  21. android:text="第2个"
  22. />
  23. </TableRow>
  24. <TableRow>
  25. <Button
  26. android:layout_width="wrap_content"
  27. android:layout_height="wrap_content"
  28. android:text="第1个"
  29. />
  30. <Button
  31. android:layout_width="wrap_content"
  32. android:layout_height="wrap_content"
  33. android:text="第2个"
  34. />
  35. <Button
  36. android:layout_width="wrap_content"
  37. android:layout_height="wrap_content"
  38. android:text="第3个"
  39. />
  40. <Button
  41. android:layout_width="wrap_content"
  42. android:layout_height="wrap_content"
  43. android:text="第4个"
  44. />
  45. <Button
  46. android:layout_width="wrap_content"
  47. android:layout_height="wrap_content"
  48. android:text="第5个"
  49. />
  50. </TableRow>
  51. <Button
  52. android:layout_width="wrap_content"
  53. android:layout_height="wrap_content"
  54. android:text="第1个"
  55. />
  56. <Button
  57. android:layout_width="wrap_content"
  58. android:layout_height="wrap_content"
  59. android:text="第2个"
  60. />
  61. </TableLayout>
  1. 2.效果图:<br />![image.png](https://cdn.nlark.com/yuque/0/2021/png/1574587/1622042956941-6643b905-cb81-4407-9c34-f326a6399ba2.png#clientId=u0a2f9ecc-f555-4&from=paste&height=468&id=u5973eb82&margin=%5Bobject%20Object%5D&name=image.png&originHeight=468&originWidth=911&originalType=binary&size=69194&status=done&style=none&taskId=u4a4d6e07-b62b-4240-99f3-d3ccca5f57d&width=911)