Android布局之表格布局

àì夳堔傛蜴生んèń 2022-04-22 00:46 445阅读 0赞

表格布局(Tablelayout)

简介:

  1. Tablelayout类以行和列的形式对控件进行管理,每一行为一个TableRow对象,或一个View控件。
  2. 当为TableRow对象时,可在TableRow下添加子控件,默认情况下,每个子控件占据一列。
  3. 当为View时,该View将独占一行。

表格布局是以行和列的形式来对控件进行管理的,所以我们来说说表格布局对行和列的确定

TableLayout的行数

在开发中由我们来直接指定,就是说有多少个TableRow对象或view控件就会有多少行。

TableLayout的列数

等于含有最多子控件的TableRow的列数。如第一(行)TableRow含2个子控件,第二(行)TableRow含3个,第三(行)TableRow含4个,那么这个表格布局的列数就是4列。

TableLayout可设置的属性

表格布局可以设置的属性有两种:全局属性、单元格属性。

全局属性(列属性): 全局属性有三个属性

Android:stretchColumns 设置可伸展的列。该列可以向行方向伸展,最多可占据一整行。

Android:shrinkColumns 设置可收缩的列。(当该列子控件里的内容太多,行内显示不完的时候会向列的方向显示内容)。

Android:collapseColumns 设置要隐藏的列。

下面就来举例说明一下:

Android:stretchColumns=”0” 第0列可伸展

Android:shrinkColumns=”1,2” 第1,2列皆可收缩

Android:collapseColumns=”1” 隐藏第一行

单元格属性: 单元格属性有两个属性

Android:layout_column 指定该单元格在第几列显示

Android:layout_span 指定该单元格占据的列数(如果我们在使用中没有指定,那么默认值将为1)

下面就来举例说明一下:

Android:layout_column=”1” 该控件在第1列

Android:layout_span=”2” 该控件占了2列

下面我们来整体运用一下表格布局里的属性(代码和效果图):

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. android:padding="3dip"
  7. >
  8. <!-- 第1个TableLayout,用于描述表中的列属性。第0列可伸展,第1列可收缩 ,第2列被隐藏-->
  9. <TextView
  10. android:text="第一个表格:全局设置:列属性设置"
  11. android:layout_height="wrap_content"
  12. android:layout_width="wrap_content"
  13. android:textSize="15sp"
  14. android:background="#7f00ffff"/>
  15. <TableLayout
  16. android:id="@+id/table1"
  17. android:layout_width="fill_parent"
  18. android:layout_height="wrap_content"
  19. android:stretchColumns="0"
  20. android:shrinkColumns="1"
  21. android:collapseColumns="2"
  22. android:padding="3dip">
  23. <TableRow>
  24. <Button android:text="该列可以伸展"/>
  25. <Button android:text="该列可以收缩"/>
  26. <Button android:text="被隐藏了"/>
  27. </TableRow>
  28. <TableRow>
  29. <TextView android:text="向行方向伸展,可以伸展很长 "/>
  30. <TextView android:text="向列方向收缩,*****************************************************************************************可以伸缩很长"/>
  31. </TableRow>
  32. </TableLayout>
  33. <!-- 第2个TableLayout,用于描述表中单元格的属性,包括:android:layout_column 及android:layout_span-->
  34. <TextView
  35. android:text="第二个:单元格设置:指定单元格属性设置"
  36. android:layout_height="wrap_content"
  37. android:layout_width="wrap_content"
  38. android:textSize="15sp"
  39. android:background="#7f00ffff"/>
  40. <TableLayout
  41. android:id="@+id/table2"
  42. android:layout_width="fill_parent"
  43. android:layout_height="wrap_content"
  44. android:padding="3dip">
  45. <TableRow>
  46. <Button android:text="第1列"/>
  47. <Button android:text="第2列"/>
  48. <Button android:text="第3列"/>
  49. </TableRow>
  50. <TableRow>
  51. <TextView android:text="指定在第2列" android:layout_column="1"/>
  52. </TableRow>
  53. <TableRow>
  54. <TextView
  55. android:text="第二列和第三列!!!!!!!!!!!!"
  56. android:layout_column="1"
  57. android:layout_span="2"
  58. />
  59. </TableRow>
  60. </TableLayout>
  61. <!-- 第3个TableLayout,使用可伸展特性布局-->
  62. <TextView
  63. android:text="第三个表格:非均匀布局,控件长度根据内容伸缩"
  64. android:layout_height="wrap_content"
  65. android:layout_width="wrap_content"
  66. android:textSize="15sp"
  67. android:background="#7f00ffff"/>
  68. <TableLayout
  69. android:id="@+id/table3"
  70. android:layout_width="fill_parent"
  71. android:layout_height="wrap_content"
  72. android:stretchColumns="*"
  73. android:padding="3dip"
  74. >
  75. <TableRow>
  76. <Button android:text="一笑山河红袖遮" ></Button>
  77. <Button android:text="姜泥"></Button>
  78. <Button android:text="两剑惊破旧山河" ></Button>
  79. </TableRow>
  80. </TableLayout>
  81. <!-- 第4个TableLayout,使用可伸展特性,并指定每个控件宽度一致,如1dip-->
  82. <TextView
  83. android:text="表4:均匀布局,控件宽度一致"
  84. android:layout_height="wrap_content"
  85. android:layout_width="wrap_content"
  86. android:textSize="15sp"
  87. android:background="#7f00ffff"/>
  88. <TableLayout
  89. android:id="@+id/table4"
  90. android:layout_width="fill_parent"
  91. android:layout_height="wrap_content"
  92. android:stretchColumns="*"
  93. android:padding="3dip"
  94. >
  95. <TableRow>
  96. <Button android:text="徐凤年" android:layout_width="1dip"></Button>
  97. <Button android:text="温华" android:layout_width="1dip"></Button>
  98. <Button android:text="天不生我李淳罡" android:layout_width="1dip"></Button>
  99. </TableRow>
  100. </TableLayout>
  101. </LinearLayout>

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MDc4MzMxNQ_size_16_color_FFFFFF_t_70

说完了怎么用,咱们再来说说表格布局的优点和缺点:

优点:

1、结构位置更简单

2、容易上手

3、 数据化的存放更合理。

例如,学生信息这样的表,相对简单,如果用别的布局的话就比较麻烦信息也比较杂乱。

缺点:

1、 标签结构多,代码复杂

2、 表格布局,不利于搜索引擎抓取信息

这就是Android常用布局中的表格布局啦~

发表评论

表情:
评论列表 (有 0 条评论,445人围观)

还没有评论,来说两句吧...

相关阅读

    相关 androidmerge布局

    <merge />标签闪亮登场了。当LayoutInflater遇到这个标签时,它会跳过它,并将<merge />内的元素添加到<merge />的父元素里。迷惑了吗?让我们用