TableLayout表格布局
文章目录
- 如何确定行数与列数
- 三个常用属性
- collapseColumns(隐藏列)
- stretchColumns(拉伸列)
- shrinkColumns(收缩列)
- 使用实例
相信学过HTML
的朋友都知道,我们可以通过<table>
、<tr>
、<td>
就可以生成一个HTML
的表格,而Android
的TableLayout
也允许我们使用表格的方式来排列组件,就是行与列的方式。但它却不像Android 4.0
后引入的GridLayout
(网格)布局一样,无法直接设置多少行与多少列!
如何确定行数与列数
如下所示:
- 如果我们直接往
TableLayout
中添加组件的话,那么这个组件将占满一行。 - 如果我们想在一行上有多个组件的话,就要添加一个
TableRow
的容器,把组件都丢到里面。 tablerow
中的组件个数就决定了该行有多少列,而列的宽度由该列中最宽的单元格决定。tablerow
的layout_width
属性默认是fill_parent
的,我们自己设置成其他的值也不会生效!但layout_height
默认是wrapten_content
的,我们可以自己设置大小。- 整个表格布局的宽度取决于父容器的宽度(占满父容器本身)。
- 有多少行就要自己数啦,一个
tablerow
是一行,一个单独的组件也是一行。多少列则是看tablerow
中的组件个数,组件最多的就是TableLayout
的列数。
三个常用属性
如下所示:
android:collapseColumns
:设置需要被隐藏的列的序号。android:shrinkColumns
:设置允许被收缩的列的列序号。android:stretchColumns
:设置运行被拉伸的列的列序号。
以上这三个属性的列号都是从0
开始算的,比如shrinkColunmns = "2"
,对应的是第三列。可以设置多个,用逗号隔开(比如0,2
)。如果是所有列都生效,则用*
号即可。
除了这三个常用属性,还有两个属性,分别是跳格子以及合并单元格,这和HTML
中的Table
类似。android:layout_column = "2"
表示的就是跳过第二个,直接显示到第三个格子处,从1
开始算。
collapseColumns(隐藏列)
在TableRow
中定义5
个按钮后,接着在最外层的TableLayout
中添加属性android:collapseColumns = "0,2"
,就是隐藏第一与第三列:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/TableLayout2"android:layout_width="fill_parent"android:layout_height="wrap_content"android:collapseColumns="0,2"><TableRow><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="one" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="two" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="three" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="four" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="five" /></TableRow>
</TableLayout>
stretchColumns(拉伸列)
在TableLayout
中设置了四个按钮,接着在最外层的TableLayout
中添加属性android:stretchColumns = "1"
,设置第二列为可拉伸列,让该列填满这一行所有的剩余空间:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/TableLayout2"android:layout_width="fill_parent"android:layout_height="wrap_content"android:stretchColumns="1"><TableRow><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="one" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="two" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="three" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="four" /></TableRow>
</TableLayout>
shrinkColumns(收缩列)
这里设置了5
个按钮和一个文本框,在最外层的TableLayout
中添加属性android:shrinkColumns = "1"
,设置第二个列为可收缩列:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/TableLayout2"android:layout_width="fill_parent"android:layout_height="wrap_content"android:shrinkColumns="1"><TableRow><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="one" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="two" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="three" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="four" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="five" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="文本XX" /></TableRow>
</TableLayout>
从图中可以看到two
这个按钮被挤压成条条状,这个就是收缩,为了保证表格能适应父容器的宽度。
使用实例
使用TableLayout
来完成简单的登录界面:
流程解析如下:
- 调用
gravity
属性,设置为center_vertical
,让布局里面的组件在竖直方向上居中。 - 将
TableLayout
中的第一和第四列设置为可拉伸。 - 在每个
TableRow
中添加两个TextView
,用于拉伸填满该行,这样可以让表格水平居中。 - 设置属性
android:stretchColumns = "0,3"
,是为了让两边都充满,那么中间部分就可以居中了。
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/TableLayout1"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#66FF66"android:gravity="center_vertical"android:stretchColumns="0,3"tools:context=".MainActivity"><TableRow><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="用户名:" /><EditTextandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:minWidth="150dp" /></TableRow><TableRow><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="密 码:" /><EditTextandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:minWidth="150dp" /></TableRow><TableRow><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="登陆" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="退出" /></TableRow>
</TableLayout>
TableLayout表格布局相关推荐
- TableLayout 表格布局,FrameLaout 帧布局 ,AbsoluteLayout绝对布局的分析
这三个布局就放在一起来写了他们用的比较少,不过为了写这遍 博客我换特意去复习了下, 第一个表格布局TableLayout 表格布局顾名思义 就是与表格类似,以行,列形式来管理其中的组件的, 它是< ...
- android表格布局最后一个组件,Android布局之TableLayout表格布局
Tablelayout类以行和列的形式对控件进行管理,每一行为一个TableRow对象,或一个View控件.当为TableRow对象时,可在TableRow下添加子控件,默认情况下,每个子控件占据一列 ...
- 安卓APP_ 布局(4) —— TableLayout表格布局
摘自:安卓APP_ 布局(4) -- TableLayout表格布局 作者:丶PURSUING 发布时间: 2021-04-11 22:55:50 网址:https://blog.csdn.net/w ...
- android html 显示表格边框,tablelayout表格布局详解
如果你已经下载好MT4软件(很多专业外汇平台都有提供下载的),在手机桌面找到图表打开,然后选择好交易商,输入账号密码就可以了. TableLayout怎样实现表格布局 表格布局的子对象不能指定 lay ...
- Android 应用开发(38)TableLayout(表格布局)
TableLayout(表格布局) 前面我们已经学习了平时实际开发中用得较多的线性布局(LinearLayout)与相对布局(RelativeLayout), 其实学完这两个基本就够用了,笔者在实际开 ...
- android简单实现表格布局,Android开发中TableLayout表格布局
Android开发中TableLayout表格布局 一.引言 在移动端应用程序开发中,常常会使用到表格布局,iOS和Android开发框架中都提供了独立的表格视图控件供开发者使用,例如iOS中的UIT ...
- 第15章、布局Layouts之TableLayout表格布局(从零开始学Android)
TableLayout表格布局 TableLayout是指将子元素的位置分配到行或列中.Android的一个TableLayout有许多TableRow组成,每一个TableRow都会定义一个Row. ...
- Android基础入门教程——2.2.3 TableLayout(表格布局)
Android基础入门教程--2.2.3 TableLayout(表格布局) 标签(空格分隔): Android基础入门教程 本节引言: 前面我们已经学习了平时实际开发中用得较多的线性布局(Linea ...
- 【Android 】零基础到飞升 | TableLayout(表格布局)
2.2.3 TableLayout(表格布局) 本节引言: 前面我们已经学习了平时实际开发中用得较多的线性布局(LinearLayout)与相对布局(RelativeLayout), 其实学完这两个基 ...
- 3.2.3 TableLayout(表格布局)
3.2.3 TableLayout(表格布局) 标签: StudyNote 本文声明: 本文由Coder-pig编写,想了解其他内容,可见CoderPig's Android Study Note-- ...
最新文章
- VC/MFC分割字符串(SplitString)返回CStringArray
- 所有的编程语言知识,都包含在这100张思维导图里了丨GitHub 13.1k星
- 函数调用oracle,oracle 函数调用
- java swing鼠标事件监听_java swing鼠标监听问题
- IDEA2020版本如何导入jar包
- 我对STL的一些看法(三)认识list容器
- java Redis Jedis存储Java对象 - (Java序列化为byte数组方式)
- android mvp框架基类,Android MVP架构项目搭建封装,基类封装
- java es scroll,Elasticsearch Scroll分页检索案例分享
- 在Zephyr上使用malloc/new
- 读 疯狂的程序员 有感
- NSA网络武器被公开,面对突发性的高危漏洞事件,我们应该如何应对?
- 拓视角丨拓宽市场边界,数智化转型构建产业新格局
- 苹果app开发流程详解
- Java集合这样子学习
- 计算机二级MS office的高频考点~
- Web AWD流程与技巧
- iOS 支付宝支付 微信支付SDK接口不统一? 盘他!
- 我国数据要素统一大市场构建的问题与对策
- 如何保存微信视频到本地,微信朋友圈怎么发本地的视频。
热门文章
- 计算机毕业设计ssm疫情状态下的图书馆座位预约系统
- output fns.php,output_fns.php
- BUUCTF-reverse-reverse3(超级详细)
- visual studio code搭建Java环境 - 一步一个脚印详细教程
- 使用EasyExcel读写Excel文件
- 微信小程序学习日志(一)
- 女儿不愿找工作在求职现场辱骂殴打母亲(图)
- iTextSharp简单生成pdf和操作pdf添加水印
- 重新启动计算机挂起状态,关于计算机重新启动处于挂起状态的问题解决
- 如何打开计算机控制版面,如何设置控制面板在“此电脑”页面显示?