Android表格布局(Table Layout)

先来看布局管理器之间继承关系图:

图1

可知TableLayout继承了LinearLayout,所以表格布局本质上依然是线性管理器。

表格布局采用行、列的形式来管理组件,它并不需要明确地声明包含了多少行、多少列,而是通过添加TableRow、其他组件来控制表格的行数和列数。

每向TableLayout添加一个TableRow,该TableRow就是一个表格行,TableRow也是容器,因此它也可以不断地添加组件,每添加一个子组件该表格就添加一列。

TableLayout一般以下面两种方式实现:

(1)  自己作为最顶层父容器

  1. <!--定义一个TableLayout,有两行
  2. 第1列所有单元格的宽度可以被收缩,以保证该表格能适应父容器的宽度
  3. 第2列所有单元格的宽度可以拉伸,以保证能完全填满表格空余空间-->
  4. <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
  5. android:id="@+id/TableLayout1"
  6. android:layout_width="wrap_content"
  7. android:layout_height="wrap_content"
  8. android:shrinkColumns="1"
  9. android:stretchColumns="2">
  10.  
  11. <!--这是此TableLayout的第1行,没有使用TableRow,直接添加一个Button,那么次Button自己占用整行 -->
  12. <Button
  13. android:id="@+id/button1"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:text="独自一行的按钮1"/>
  17.  
  18. <!-- 这是第2行,先添加一个TableRow,并为TableRow添加三个Button,也就是此行包含三列 -->
  19. <TableRow>
  20. <Button
  21. android:id="@+id/button2"
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:text="普通按钮1"/>
  25.  
  26. <Button
  27. android:id="@+id/button3"
  28. android:layout_width="wrap_content"
  29. android:layout_height="wrap_content"
  30. android:text="被收缩的按钮1"/>
  31.  
  32. <Button
  33. android:id="@+id/button4"
  34. android:layout_width="wrap_content"
  35. android:layout_height="wrap_content"
  36. android:text="被拉伸的按钮1"/>
  37. </TableRow>
  38.  
  39. <!--这是此TableLayout的第3行,没有使用TableRow,直接添加一个Button,那么次Button自己占用整行 -->
  40. <Button
  41. android:id="@+id/button5"
  42. android:layout_width="wrap_content"
  43. android:layout_height="wrap_content"
  44. android:text="独自一行的按钮2"/>
  45.  
  46. <!-- 这是第4行,先添加一个TableRow,并为TableRow添加三个Button,也就是此行包含三列 -->
  47. <TableRow>
  48. <Button
  49. android:id="@+id/button6"
  50. android:layout_width="wrap_content"
  51. android:layout_height="wrap_content"
  52. android:text="普通按钮2"/>
  53.  
  54. <Button
  55. android:id="@+id/button7"
  56. android:layout_width="wrap_content"
  57. android:layout_height="wrap_content"
  58. android:text="被收缩的按钮2"/>
  59.  
  60. <Button
  61. android:id="@+id/button8"
  62. android:layout_width="wrap_content"
  63. android:layout_height="wrap_content"
  64. android:text="被拉伸的按钮2"/>
  65. </TableRow>
  66.  
  67. </TableLayout>

效果如下:

图2

这里只有一个TableLayout,如果我们想单独控制地4行,比如想把“普通按钮2”隐藏,也就是增加android:collapseColumns="0",这样会把“普通按钮1”,这一列也隐藏了,如下图:

图3

但如果要实现只“普通按钮2”这列,我们来看下面的实现

(2)  LinearLayout作为TableLayout的容器

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:orientation="vertical"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent">-->
  6.  
  7. <!--定义第1个TableLayout,有两行
  8. 第1列所有单元格的宽度可以被收缩,以保证该表格能适应父容器的宽度
  9. 第2列所有单元格的宽度可以拉伸,以保证能完全填满表格空余空间-->
  10. <TableLayout
  11. android:id="@+id/TableLayout1"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:shrinkColumns="1"
  15. android:stretchColumns="2">
  16.  
  17. <!--这是此TableLayout的第1行,没有使用TableRow,直接添加一个Button,那么次Button自己占用整行 -->
  18. <Button
  19. android:id="@+id/button1"
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:text="独自一行的按钮1"/>
  23.  
  24. <!-- 这是第2行,先添加一个TableRow,并为TableRow添加三个Button,也就是此行包含三列 -->
  25. <TableRow>
  26. <Button
  27. android:id="@+id/button2"
  28. android:layout_width="wrap_content"
  29. android:layout_height="wrap_content"
  30. android:text="普通按钮1"/>
  31.  
  32. <Button
  33. android:id="@+id/button3"
  34. android:layout_width="wrap_content"
  35. android:layout_height="wrap_content"
  36. android:text="被收缩的按钮1"/>
  37.  
  38. <Button
  39. android:id="@+id/button4"
  40. android:layout_width="wrap_content"
  41. android:layout_height="wrap_content"
  42. android:text="被拉伸的按钮1"/>
  43. </TableRow>
  44.  
  45. </TableLayout>
  46. <!--定义第2个TableLayout,有两行
  47. 第1列所有单元格的宽度可以被收缩,以保证该表格能适应父容器的宽度
  48. 第2列所有单元格的宽度可以拉伸,以保证能完全填满表格空余空间-->
  49. <TableLayout
  50. android:id="@+id/TableLayout2"
  51. android:layout_width="wrap_content"
  52. android:layout_height="wrap_content"
  53. android:collapseColumns="0"
  54. android:shrinkColumns="1"
  55. android:stretchColumns="2">
  56.  
  57. <!--这是此TableLayout的第3行,没有使用TableRow,直接添加一个Button,那么次Button自己占用整行 -->
  58. <Button
  59. android:id="@+id/button5"
  60. android:layout_width="wrap_content"
  61. android:layout_height="wrap_content"
  62. android:text="独自一行的按钮2"/>
  63.  
  64. <!-- 这是第4行,先添加一个TableRow,并为TableRow添加三个Button,也就是此行包含三列 -->
  65. <TableRow>
  66. <Button
  67. android:id="@+id/button6"
  68. android:layout_width="wrap_content"
  69. android:layout_height="wrap_content"
  70. android:text="普通按钮2"/>
  71.  
  72. <Button
  73. android:id="@+id/button7"
  74. android:layout_width="wrap_content"
  75. android:layout_height="wrap_content"
  76. android:text="被收缩的按钮2"/>
  77.  
  78. <Button
  79. android:id="@+id/button8"
  80. android:layout_width="wrap_content"
  81. android:layout_height="wrap_content"
  82. android:text="被拉伸的按钮2"/>
  83. </TableRow>
  84.  
  85. </TableLayout>
  86.  
  87. </LinearLayout>

效果如下:

图4

通过在第2个TableLayout中增加android:collapseColumns="0"实现,这里需要主要的是LinearLayout的android:orientation属性值的设置,如果没有这一项或是其值为horizontal,那么后面两行都看不到,因为是以水平方向排列的,后面两行显示在前两行的右边,看不到。

Android表格布局(Table Layout)的更多相关文章

  1. Android表格布局之设置边框

    Android表格布局本身没有边框,不过可以通过背景色的设置可以实现表格边框的显示. 首先可以设置TableRow的背景色,然后设置内容的背景色.根据它们的颜色差就出现了边框.只要微调Content与 ...

  2. Android线性布局(Linear Layout)

    Android线性布局(Linear Layout) LinearLayout是一个view组(view group),其包含的所有子view都以一个方向排列,垂直或是水平方向.我们能够用androi ...

  3. Android帧布局(Frame Layout)

    Android帧布局(Frame Layout) FrameLayout是最简单的一个布局管理器.FrameLayout为每个加入其中的组件创建一个空白区域(一帧),这些组件根据layout_grav ...

  4. [android] 表格布局和绝对布局

    /*****************2016年4月28日 更新*************************************/ 知乎:为什么Android没有像iOS一样提供autolay ...

  5. Android 表格布局<TableLayout>

    表格布局即,tableLayout,表格布局通过行.列的形式来管理UI组件,TablelLayout并不需要明确地声明包含多少行.多少列,而是通过TableRow,以及其他组件来控制表格的行数和列数, ...

  6. 有间距的表格布局 table布局

    1.先看有间距的布局效果: 2.少说多做,直接看代码(代码中有注释) <!DOCTYPE html> <html lang="zh"> <head&g ...

  7. Android 表格布局 TableLayout

    属性介绍 stretchColumns:列被拉伸 shrinkColumns:列被收缩 collapseColumns:列被隐藏 举例测试 <TableLayout android:id=&qu ...

  8. Android View 布局流程(Layout)完全解析

    前言 上一篇文章,笔者详细讲述了View三大工作流程的第一个,Measure流程,如果对测量流程还不熟悉的读者可以参考一下上一篇文章.测量流程主要是对View树进行测量,获取每一个View的测量宽高, ...

  9. AndroidのUI布局之layout weight

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

随机推荐

  1. The new powerful SQL executing schedule monthly or weekly in DB Query Analyzer 7.01

    1 About DB Query Analyzer DB Query Analyzer is presented by Master Genfeng,Ma from Chinese Mainland. ...

  2. Hadoop就业面试题

    ----------------------------------------------------------------------------- [申明:资料来源于互联网] 本文链接:htt ...

  3. shiro架构

    1 shiro介绍  1.1 什么是shiro 分享牛系列,分享牛专栏,分享牛.shiro是apache旗下一个开源框架,它将软件系统的安全认证相关的功能抽取出来,实现用户身份认证,权限授权.加密.会 ...

  4. iOS 10.0之前和之后的Local Notification有神马不同

    在iOS 10.0之前apple还没有将通知功能单独拿出来自成一系.而从10.0开始原来的本地通知仍然可用,只是被标记为过时.于是乎我们可以使用10.0全新的通知功能.别急-让我们慢慢来,先从iOS ...

  5. Scikit-learn:分类classification

    http://blog.csdn.net/pipisorry/article/details/53034340 支持向量机SVM分类 svm分类有多种不同的算法.SVM是非常流行的机器学习算法,主要用 ...

  6. Racket 模拟SICP的流(延时计算)

    默认的Racket是要对函数参数进行求值的, 例如(f 1 (+ 1 2))里面,(+ 1 2)要先求值为3,变为(f 1 3)再进行下一步操作.因此, Racket若按照SICP使用define关键 ...

  7. UIScrollView实现图片轮播器的无限滚动

    简介 在现在的一些App中常常见到图片轮播器,一般用于展示广告.新闻等数据,在iOS内并没有现成的控件直接实现这种功能,但是通过UIScrollView的允许分页设置,可以实现滚动轮播的功能. 轮播原 ...

  8. Android简易实战教程--第十八话《ListView显示,简单的适配器SimpleAdapter》

    本篇介绍Listview的显示,对于listview有许多的适配器,如ArrayAdapter,BaseAdapter,SimpleAdapter等等.本篇先热身一下,介绍最简单的SimpleAdap ...

  9. introduction of velocity

    一.velocity 简介 基于java 的模板引擎,apache 旗下的开源软件项目. 目的在于隔离 表示层和业务逻辑层,当然现在做的不仅仅是这些. 二.应用场景 web 应用程序:创建html页面 ...

  10. Android 访问assets下的文件

    assets下经常可以放一些比较大的资源,对于这些资源我们如何访问. 步骤 1.获取AssetManager. AssetManager am = getResources().getAssets() ...