TableLayout 和 GridLayout 的区别

  • TableLayout 和 GridLayout 的区别

    • 一. TableLayout
    • 二. GridLayout
      • 2.1 隔行/列的效果
    • 三. 总结

一. TableLayout

  • 继承LinearLayout
  • 不需要明确的申明包含多少行多少列
  • 通过添加 TableRow / 其他组件 来控制表格的行数和列数
  • 单元格设置格式有3种:Shrinkable(可收缩),Stretchable(可拉伸),Collapsed(隐藏的)

运用TableLayout只能通过添加TableRow或者组件来增加行和列。当直接添加组件的时候,组件独自占用一行。当添加TableRow时,该布局增加了一行,并且在TableRow里每添加一个组件,便增加一列。

TableLayout无法做出跨行跨列的效果,每行每列都是挨着的,就算是单元格设置Collapsed属性,如果后面有组件,也会填充上来。

<!--定义一个表格布局,指定第2 列可收缩,第3 列隐藏,第4列可拉伸--><TableLayout
        android:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/table01"android:shrinkColumns="1"android:collapseColumns="2"android:stretchColumns="3"><!--直接添加一个按钮,会直接占用一行--><Button android:id="@+id/ok1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="独自一行的按钮ok1"/><!--在TableRow添加加一个按钮,会添加一列--><TableRow><Button android:id="@+id/ok2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="普通按钮"/><Button android:id="@+id/ok3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="shrink收缩按钮"/><Button android:id="@+id/ok4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="collapsed隐藏按钮"/><Button android:id="@+id/ok5"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="stretch拉伸按钮"/></TableRow></TableLayout>

效果如下图:

可以看出:
- 隐藏的按钮不会空着,会被后面的组件填上去
- 收缩会使该列宽度可以被收缩,以保证该table能适应父容器的宽度
- 拉伸可以使该列的所有单元格宽度可以被拉伸,以保证组件能完全填满表格空余空间

二. GridLayout

  • Android4.0之后新加的布局管理器
  • 能够把整个容器划分为rows*columns的网格,每个网格可以放置一个组件
  • 可以设置一个组件横跨多少列或者纵跨多少行
  • 当单元格大小大于组件大小时,可以通过设置layout_grativity属性值,设置组件在单元格里的位置
  • 通过设置layout_columnWeight/layout_rowWeight属性值,可以设置各个组件的大小比例

2.1 隔行/列的效果

方法1.设置margin等属性值
方法2.让组件占两列或者两行,设置layout_grativity为left/right
代码如下:

<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent" android:layout_height="match_parent"android:rowCount="6"android:columnCount="4"android:id="@+id/root"><TextView
        android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_columnSpan="4"android:textSize="50sp"android:layout_marginLeft="2pt"android:layout_marginRight="2pt"android:padding="3pt"android:gravity="right"android:layout_gravity="right"android:background="#eee"android:textColor="#000"android:text="0" /><Button
        android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_columnSpan="4"android:text="Clear"android:textSize="30sp"/><Button
        android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_column="0"android:layout_row="2"android:layout_rowSpan="2"android:layout_gravity="fill"android:layout_marginRight="10pt"android:text="1"android:textSize="40dp"/><Button
        android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_columnSpan="2"android:layout_gravity="left"android:text="2"android:textSize="40dp"/><Button
        android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="3"android:textSize="40dp"/><Button
        android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_columnSpan="2"android:layout_gravity="left"android:text="4"android:textSize="40dp"/><Button
        android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="5"android:textSize="40dp"/><Button
        android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_column="1"android:layout_row="4"android:layout_columnSpan="2"android:layout_gravity="left"android:text="6"android:textSize="40dp"/><Button
        android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="7"android:textSize="40dp"/>
</GridLayout>

效果图:

三. 总结

  1. TableLayout可以通过设置layout_margin参数做出隔行隔列的效果,GridLayout不仅可以通过layout_margin,还可以利用rowSpan/columnSpan合并单元格以及设置layout_grativity来达成效果。这第二种方法的效果更好。
  2. TableLayout不能跨行跨列,不灵活,GridLayout能够更好实现隔行/隔列效果也是因为这个特点。
  3. 不能同时向水平和垂直方向做控件的对齐
    TableLayout继承了LinearLayout,因此只能向一个方向做控件的对齐。.

TableLayout 和 GridLayout 的区别相关推荐

  1. Android中常见五种布局管理器——RelativeLayout、LinearLayout、FrameLayout、TableLayout、GridLayout

    目录 布局管理器 RelativeLayout 常见属性 Relative的实践操作(实现软件更新界面) LinearLayout 常见属性 LinearLayout的实践操作(模范登录以及微信底部) ...

  2. 如何制作表格(一)——TableLayout

    一.Android中能够用于网格布局的控件(制作各种这样的表格) Android提供了四种方式来制作表格,分别为: TableLayout.GridLayout.GridView.Recycler中的 ...

  3. Android:Android学习路线图

    https://blog.csdn.net/libing1991_/article/details/53455243 前言 工作快一年,Android完全靠自学,看着那些基础教程一步步去做,写过简单的 ...

  4. Android界面开发基础

    前言 安卓开发(指原生开发)和普通的Java开发,web开发最大的不同是什么?界面.老早的Java awt/swing以及现在的JavaFX,里面的UI库和安卓完全不是一个体系.至于web开发的各种U ...

  5. Android开发指南中文版

    Android开发指南中文版 -应用程序框架   iefreer@hotmail.com 2009/9/10 个人主页: http://blog.csdn.net/iefreer 本文是对Androi ...

  6. (转载)非常好 必须要顶

    关键类 1. Activity 2. Service 3. BroadcastReceiver 4. ContentProvider 5. Intent Android应用程序使用Java做为开发语言 ...

  7. Android常见知识点

    什么是 3G 3G,全称为3rd Generation,中文含义就是指第三代数字通信. 所谓3G,是指将无线通信与国际互联网等多媒体通信结合的新一代移动通信系统. 3G只是一种通信技术标准,符合这个标 ...

  8. 一篇讲的很细很好的帖子

    关键类 1. Activity  2. Service  3. BroadcastReceiver  4. ContentProvider  5. Intent  Android应用程序使用Java做 ...

  9. Android中文开发教程()

    本文是对Android SDK1.5版的英文开发资料Android Development Guide一文应用程序框架部分的翻译,覆盖了Android应用开发所有主要的概念.部分内容整理自网络.本文仅 ...

最新文章

  1. 2021年大数据Spark(十二):Spark Core的RDD详解
  2. 阿里规范不建议多表join,可这SQL要怎么写啊?
  3. R语言ggplot2可视化绘制分组水平并行条形图(bar plot)并为条形图内添加标签
  4. 希腊字母常用指代意义及其中文读音
  5. 推荐搜索系统论文干货集锦(持续更新)
  6. axios代理跨域 cli4_跨域本质及解决办法
  7. Milking Time(POJ-3616)
  8. Error:The module 'app' is an Android project without build variants, and cannot be built.
  9. java 方法的重载_Java中的方法和方法重载
  10. NWT与HHTH两个公司的对比
  11. c语言答辩ppt案例,c语言ppt例子课题了答辩ppt成品中南民族大学.ppt
  12. 《小5自我推荐资源 | 寻找C站“宝藏》
  13. 安卓高级8 支付宝支付
  14. linux忘记root密码怎么办——重置root密码的四种方法
  15. Java筑基——反射(1):基本类周边信息获取
  16. 华师计算机基础在线作业秋,18秋华师《计算机基础》在线作业(标准答案).doc...
  17. 尚硅谷大数据技术Scala教程-笔记04【集合】
  18. Android fragment回退栈
  19. 机器学习的数学基础(一)
  20. OLED显示屏 IIC/I²C/I2C通信 128*64分辨率 SH1106 / SSD1306 驱动教程

热门文章

  1. missing whitespace after ‘,‘
  2. 表哥表妹们,Excel2019 给我们带来好东西了-新函数帮你来了!
  3. 电源管理芯片的产业链概述及灵敏
  4. 海外社交媒体营销:facebook广告文案的8个写作技巧分享
  5. 人机交互的新方向:智能聊天机器人
  6. 公务员面试理论与实务笔记(2)
  7. 【871. 最低加油次数】
  8. sql显示结果不要科学计数法_教你一招丨标准平板菌落计数法
  9. 对路径“”的访问被拒绝
  10. 内蒙古海天公司企业网的规划与设计