Android表格布局(Table Layout)
Android表格布局(Table Layout)
先来看布局管理器之间继承关系图:
图1
可知TableLayout继承了LinearLayout,所以表格布局本质上依然是线性管理器。
表格布局采用行、列的形式来管理组件,它并不需要明确地声明包含了多少行、多少列,而是通过添加TableRow、其他组件来控制表格的行数和列数。
每向TableLayout添加一个TableRow,该TableRow就是一个表格行,TableRow也是容器,因此它也可以不断地添加组件,每添加一个子组件该表格就添加一列。
TableLayout一般以下面两种方式实现:
(1) 自己作为最顶层父容器
<!--定义一个TableLayout,有两行
第1列所有单元格的宽度可以被收缩,以保证该表格能适应父容器的宽度
第2列所有单元格的宽度可以拉伸,以保证能完全填满表格空余空间-->
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/TableLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:shrinkColumns="1"
android:stretchColumns="2"> <!--这是此TableLayout的第1行,没有使用TableRow,直接添加一个Button,那么次Button自己占用整行 -->
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="独自一行的按钮1"/> <!-- 这是第2行,先添加一个TableRow,并为TableRow添加三个Button,也就是此行包含三列 -->
<TableRow>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="普通按钮1"/> <Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="被收缩的按钮1"/> <Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="被拉伸的按钮1"/>
</TableRow> <!--这是此TableLayout的第3行,没有使用TableRow,直接添加一个Button,那么次Button自己占用整行 -->
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="独自一行的按钮2"/> <!-- 这是第4行,先添加一个TableRow,并为TableRow添加三个Button,也就是此行包含三列 -->
<TableRow>
<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="普通按钮2"/> <Button
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="被收缩的按钮2"/> <Button
android:id="@+id/button8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="被拉伸的按钮2"/>
</TableRow> </TableLayout>
效果如下:
图2
这里只有一个TableLayout,如果我们想单独控制地4行,比如想把“普通按钮2”隐藏,也就是增加android:collapseColumns="0",这样会把“普通按钮1”,这一列也隐藏了,如下图:
图3
但如果要实现只“普通按钮2”这列,我们来看下面的实现
(2) LinearLayout作为TableLayout的容器
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">--> <!--定义第1个TableLayout,有两行
第1列所有单元格的宽度可以被收缩,以保证该表格能适应父容器的宽度
第2列所有单元格的宽度可以拉伸,以保证能完全填满表格空余空间-->
<TableLayout
android:id="@+id/TableLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:shrinkColumns="1"
android:stretchColumns="2"> <!--这是此TableLayout的第1行,没有使用TableRow,直接添加一个Button,那么次Button自己占用整行 -->
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="独自一行的按钮1"/> <!-- 这是第2行,先添加一个TableRow,并为TableRow添加三个Button,也就是此行包含三列 -->
<TableRow>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="普通按钮1"/> <Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="被收缩的按钮1"/> <Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="被拉伸的按钮1"/>
</TableRow> </TableLayout>
<!--定义第2个TableLayout,有两行
第1列所有单元格的宽度可以被收缩,以保证该表格能适应父容器的宽度
第2列所有单元格的宽度可以拉伸,以保证能完全填满表格空余空间-->
<TableLayout
android:id="@+id/TableLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:collapseColumns="0"
android:shrinkColumns="1"
android:stretchColumns="2"> <!--这是此TableLayout的第3行,没有使用TableRow,直接添加一个Button,那么次Button自己占用整行 -->
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="独自一行的按钮2"/> <!-- 这是第4行,先添加一个TableRow,并为TableRow添加三个Button,也就是此行包含三列 -->
<TableRow>
<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="普通按钮2"/> <Button
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="被收缩的按钮2"/> <Button
android:id="@+id/button8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="被拉伸的按钮2"/>
</TableRow> </TableLayout> </LinearLayout>
效果如下:
图4
通过在第2个TableLayout中增加android:collapseColumns="0"实现,这里需要主要的是LinearLayout的android:orientation属性值的设置,如果没有这一项或是其值为horizontal,那么后面两行都看不到,因为是以水平方向排列的,后面两行显示在前两行的右边,看不到。
Android表格布局(Table Layout)的更多相关文章
- Android表格布局之设置边框
Android表格布局本身没有边框,不过可以通过背景色的设置可以实现表格边框的显示. 首先可以设置TableRow的背景色,然后设置内容的背景色.根据它们的颜色差就出现了边框.只要微调Content与 ...
- Android线性布局(Linear Layout)
Android线性布局(Linear Layout) LinearLayout是一个view组(view group),其包含的所有子view都以一个方向排列,垂直或是水平方向.我们能够用androi ...
- Android帧布局(Frame Layout)
Android帧布局(Frame Layout) FrameLayout是最简单的一个布局管理器.FrameLayout为每个加入其中的组件创建一个空白区域(一帧),这些组件根据layout_grav ...
- [android] 表格布局和绝对布局
/*****************2016年4月28日 更新*************************************/ 知乎:为什么Android没有像iOS一样提供autolay ...
- Android 表格布局<TableLayout>
表格布局即,tableLayout,表格布局通过行.列的形式来管理UI组件,TablelLayout并不需要明确地声明包含多少行.多少列,而是通过TableRow,以及其他组件来控制表格的行数和列数, ...
- 有间距的表格布局 table布局
1.先看有间距的布局效果: 2.少说多做,直接看代码(代码中有注释) <!DOCTYPE html> <html lang="zh"> <head&g ...
- Android 表格布局 TableLayout
属性介绍 stretchColumns:列被拉伸 shrinkColumns:列被收缩 collapseColumns:列被隐藏 举例测试 <TableLayout android:id=&qu ...
- Android View 布局流程(Layout)完全解析
前言 上一篇文章,笔者详细讲述了View三大工作流程的第一个,Measure流程,如果对测量流程还不熟悉的读者可以参考一下上一篇文章.测量流程主要是对View树进行测量,获取每一个View的测量宽高, ...
- AndroidのUI布局之layout weight
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...
随机推荐
- RX系列一 | ReactiveX根源 | 观察者模式分析
RX系列一 | ReactiveX根源 | 观察者模式分析 Rx的响应式编程算是很火了,对吧,但是我的工作基本上就不会接触,所以学习的比较晚,到现在才分享给大家,我们一点点的去学,当你看完这整个系列的 ...
- vue关于数组使用的坑
关于数组使用的坑 https://vuejs.org/v2/guide/list.html#Caveats 简言之, 不要使用a[i] = v 的形式, 用a.splice(i, 1, v), 或Vu ...
- Oracle常用语句语法汇总
第一篇 基本操作 --解锁用户 alter user 用户 account unlock; --锁定用户 alter user 用户 account lock; alter user sco ...
- Android艺术开发探索第三章——View的事件体系(上)
Android艺术开发探索第三章----View的事件体系(上) 我们继续来看这本书,因为有点长,所以又分了上下,你在本片中将学习到 View基础知识 什么是View View的位置参数 Motion ...
- 数组中的数分为两组,让给出一个算法,使得两个组的和的差的绝对值最小,数组中的数的取值范围是0<x<100,元素个数也是大于0, 小于100 。
比如a[]={2,4,5,6,7},得出的两组数{2,4,6}和{5,7},abs(sum(a1)-sum(a2))=0: 比如{2,5,6,10},abs(sum(2,10)-sum(5,6))=1 ...
- AR模块常用函数
--AR模块常用函数 FUNCTION get_fnd_user_name ( p_user_id IN NUMBER ) return VARCHAR2 IS CURSOR c_user_name ...
- C++ ifstream,ofstream读写二进制文件
为什要吧数据存为二进制 这个嘛,是我个人习惯,一般,我们会把日志文件存为文本文件.数据文件存成二进制文件. 其实,我们接触的文件,比如图像.视频都是以二进制的形式存储的,要想查看这类数据,必须知道数据 ...
- UNIX网络编程——原始套接字(dos攻击)
原始套接字(SOCK_RAW).应用原始套接字,我们可以编写出由TCP和UDP套接字不能够实现的功能. 注意原始套接字只能够由有 root权限的人创建. 可以参考前面的博客<<UNIX网络 ...
- Android Studio安装Genymotion插件
Android Studio安装Genymotion插件 Eclipse就不介绍了,谷歌都已经放弃Eclipse了,你还在坚持什么. 安装Genymotion 官网:https://www.genym ...
- 【NPR】铅笔画
写在前面 今天打算写一篇跟Unity基本无关的文章.起因是我上个星期不知怎么的搜到了一个网站 ,里面实现的效果感觉挺好的,后来发现是2012年的NPAR会议的最佳论文.看了下文章,觉得不是很难,就想着 ...