TableLayout(表格布局)

简介: 前面我们已经学习了平时实际开发中用得较多的线性布局(LinearLayout)与相对布局(RelativeLayout),其实学完这两个基本就够用了,这一节我们会学习Android中的第三个布局:TableLayout(表格布局)!

前面我们已经学习了平时实际开发中用得较多的线性布局(LinearLayout)与相对布局(RelativeLayout),其实学完这两个基本就够用了,这一节我们会学习Android中的第三个布局:TableLayout(表格布局)!

1.本节学习路线图

网络异常,图片无法展示
|

路线图分析:从上面的路线图,可以看出TableLayout的用法还是很简单的,无非就是确定表格的行数,以及使用那三个属性来设置每一行中的第某列的元素隐藏,拉伸,或者收缩即可!

2.TableLayout的介绍

相信学过HTML的朋友都知道,我们可以通过< table >< tr >< td >就可以生成一个HTML的表格,而Android中也允许我们使用表格的方式来排列组件,就是行与列的方式,就说我们这节的TableLayout!但却不像我们后面会讲到的Android 4.0后引入的GridLayout(网格)布局一样,直接就可以设置多少行与多少列!

3.如何确定行数与列数

  • ①如果我们直接往TableLayout中添加组件的话,那么这个组件将占满一行!!!
  • ②如果我们想一行上有多个组件的话,就要添加一个TableRow的容器,把组件都丢到里面!
  • ③tablerow中的组件个数就决定了该行有多少列,而列的宽度由该列中最宽的单元格决定
  • ④tablerow的layout_width属性,默认是fill_parent的,我们自己设置成其他的值也不会生效!!!但是layout_height默认是wrapten——content的,我们却可以自己设置大小!
  • ⑤整个表格布局的宽度取决于父容器的宽度(占满父容器本身)
  • ⑥有多少行就要自己数啦,一个tablerow一行,一个单独的组件也一行!多少列则是看tableRow中的组件个数,组件最多的就是TableLayout的列数

4.三个常用属性

android:collapseColumns:设置需要被隐藏的列的序号android:shrinkColumns:设置允许被收缩的列的列序号android:stretchColumns:设置运行被拉伸的列的列序号

以上这三个属性的列号都是从0开始算的,比如shrinkColunmns = "2",对应的是第三列!可以设置多个,用逗号隔开比如"0,2",如果是所有列都生效,则用"*"号即可除了这三个常用属性,还有两个属性,分别就是跳格子以及合并单元格,这和HTML中的Table类似:

android:layout_column="2":表示的就是跳过第二个,直接显示到第三个格子处,从1开始算的!android:layout_span="4":表示合并4个单元格,也就说这个组件占4个单元格

属性使用示例:

①collapseColumns(隐藏列)

流程:在TableRow中定义5个按钮后,接着在最外层的TableLayout中添加以下属性:android:collapseColumns = "0,2",就是隐藏第一与第三列,代码如下:

<TableLayout  
    android:id="@+id/TableLayout2"  
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"  
    android:collapseColumns="0,2" >  
    <TableRow>  
        <Button  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="one" />  
        <Button  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="two" />  
        <Button  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="three" />  
        <Button  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="four" />  
        <Button  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="five" />  
    </TableRow>  
</TableLayout>

运行效果图:

网络异常,图片无法展示
|

②stretchColumns(拉伸列)

流程:在TableLayout中设置了四个按钮,接着在最外层的TableLayout中添加以下属性:android:stretchColumns = "1"

设置第二列为可拉伸列,让该列填满这一行所有的剩余空间,代码如下:

<TableLayout    
    android:id="@+id/TableLayout2"    
    android:layout_width="fill_parent"    
    android:layout_height="wrap_content"    
    android:stretchColumns="1" >    
    <TableRow>    
        <Button    
            android:layout_width="wrap_content"    
            android:layout_height="wrap_content"    
            android:text="one" />    
        <Button    
            android:layout_width="wrap_content"    
            android:layout_height="wrap_content"    
            android:text="two" />    
        <Button    
            android:layout_width="wrap_content"    
            android:layout_height="wrap_content"    
            android:text="three" />    
        <Button    
            android:layout_width="wrap_content"    
            android:layout_height="wrap_content"    
            android:text="four" />                 
    </TableRow>    
</TableLayout>

运行效果图:

网络异常,图片无法展示
|

③shrinkColumns(收缩列)

步骤:这里为了演示出效果,设置了5个按钮和一个文本框,在最外层的TableLayout中添加以下属性:android:shrinkColumns = "1"

设置第二个列为可收缩列,代码如下:

<TableLayout  
    android:id="@+id/TableLayout2"  
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"  
    android:shrinkColumns="1" >  
    <TableRow>  
        <Button  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="one" />  
        <Button  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="two" />  
        <Button  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="three" />  
        <Button  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="four" />  
        <Button  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="five" />  
        <TextView  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="文本XX" />  
    </TableRow>  
</TableLayout>

运行截图:

网络异常,图片无法展示
|

从图中我们可以看到two这个按钮被挤压成条条状,这个就是收缩,为了保证表格能适应父容器的宽度!至于另外两个属性就不讲解了,用法和HTML相同!有兴趣的可以研究下!

5.使用实例

使用TableLayout来完成简单的登录界面,运行效果图如下:

网络异常,图片无法展示
|

流程解析:

①调用gravity属性,设置为center_vertical,让布局里面的组件在竖直方向上居中

②将TableLayout中的第一和第四列设置为可拉伸

③在每个TableRow中添加两个TextView,用于拉伸填满该行,这样可以让表格水平居中

android:stretchColumns="0,3" 设置为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"    
    tools:context=".MainActivity"     
    android:stretchColumns="0,3"    
    android:gravity="center_vertical"    
    android:background="#66FF66"    
    >    
    <TableRow>    
        <TextView />    
        <TextView     
            android:layout_width="wrap_content"    
            android:layout_height="wrap_content"    
            android:text="用户名:"/>    
        <EditText     
            android:layout_width="wrap_content"    
            android:layout_height="wrap_content"    
            android:minWidth="150dp"/>    
        <TextView />    
    </TableRow>    
    <TableRow>    
        <TextView />    
        <TextView     
            android:layout_width="wrap_content"    
            android:layout_height="wrap_content"    
            android:text="密  码:"        
        />    
        <EditText     
            android:layout_width="wrap_content"    
            android:layout_height="wrap_content"    
            android:minWidth="150dp"        
        />    
        <TextView />    
    </TableRow>    
    <TableRow>    
        <TextView />    
        <Button     
            android:layout_width="wrap_content"    
            android:layout_height="wrap_content"    
            android:text="登陆"/>    
        <Button    
            android:layout_width="wrap_content"    
            android:layout_height="wrap_content"    
            android:text="退出"/>    
        <TextView />    
    </TableRow>    
</TableLayout>

6.发现的问题

相信大家在使用这个这TableLayout的TableRow的时候会遇到这个警告:

网络异常,图片无法展示
|

当然,程序还是可以运行的,不过或许你是强迫症患者,看到黄色感叹号你就不爽的话!而解决这个警告的方法也是很奇葩的:只要你的TableLayout里面有2个或以上的TableRow就可以了!

相关文章
|
9月前
|
机器学习/深度学习 人工智能 自然语言处理
《剖析Transformer架构:自然语言处理飞跃的幕后英雄》
Transformer架构自2017年提出以来,凭借自注意力机制革新了自然语言处理(NLP)。它摒弃传统RNN的顺序处理方式,实现全局并行计算,大幅提升训练效率。通过多头自注意力机制,Transformer能精准捕捉长距离依赖关系,多维度挖掘语义信息。位置编码赋予其序列顺序感知能力,而大规模预训练则使其具备强大的通用语言能力。Transformer已成为NLP领域的核心驱动力,推动智能语音助手、机器翻译等应用进入新时代。
296 2
|
10月前
|
存储 Kubernetes 关系型数据库
阿里云ACK备份中心,K8s集群业务应用数据的一站式灾备方案
本文源自2024云栖大会苏雅诗的演讲,探讨了K8s集群业务为何需要灾备及其重要性。文中强调了集群与业务高可用配置对稳定性的重要性,并指出人为误操作等风险,建议实施周期性和特定情况下的灾备措施。针对容器化业务,提出了灾备的新特性与需求,包括工作负载为核心、云资源信息的备份,以及有状态应用的数据保护。介绍了ACK推出的备份中心解决方案,支持命名空间、标签、资源类型等维度的备份,并具备存储卷数据保护功能,能够满足GitOps流程企业的特定需求。此外,还详细描述了备份中心的使用流程、控制台展示、灾备难点及解决方案等内容,展示了备份中心如何有效应对K8s集群资源和存储卷数据的灾备挑战。
|
12月前
|
人工智能 自然语言处理 文字识别
部署《文档智能 & RAG》解决方案
部署《文档智能 & RAG》解决方案
367 4
|
数据可视化 数据挖掘 API
Python中的数据可视化利器:Matplotlib与Seaborn对比解析
在Python数据科学领域,数据可视化是一个重要环节。它不仅帮助我们理解数据,更能够让我们洞察数据背后的故事。本文将深入探讨两种广泛使用的数据可视化库——Matplotlib与Seaborn,通过对比它们的特点、优劣势以及适用场景,为读者提供一个清晰的选择指南。无论是初学者还是有经验的开发者,都能从中找到有价值的信息,提升自己的数据可视化技能。
556 3
|
缓存 Kubernetes 测试技术
在k8S中,镜像下载策略有哪些?
在k8S中,镜像下载策略有哪些?
|
弹性计算 安全 Ubuntu
无影云桌面流量费用、安全策略、软件硬件设置和全面评测
无影云桌面流量费用、安全策略、软件硬件设置和全面评测
无影云桌面流量费用、安全策略、软件硬件设置和全面评测
|
JSON 小程序 JavaScript
【微信小程序开发小白零基础入门】微信小程序界面API详解学习笔记【建议收藏】
文章目录【微信小程序开发小白零基础入门】微信小程序界面API【建议收藏】一、 交互反馈1. 消息提示框1.1.1 显示消息提示框1.1.2 关闭消息提示框2. 加载提示框2.2.1 显示加载提示框2.2.2 关闭加载提示框3. 模态弹窗4. 操作菜单二、 导航条设置2.1 当前页面标题设置2.2 导航条加载动画2.3 导航条颜色设置三、 tabBar设置3.1 tabBar标记3.1.1 设置tabBar标记3.1.2 移除tabBar标记3.2 tabBar红点3.2.1 显示tabBar红点3.2.2 隐藏tabBar红点3.3 onTabIte文章目录【微信小程序开发小白零基础入门】微信
【微信小程序开发小白零基础入门】微信小程序界面API详解学习笔记【建议收藏】
|
程序员
网传字节跳动 TNS 部门一锅端,转岗“难于上青天”
大家好,我是鸭血粉丝,不知道大家最近有没有看到一个帖子,说的是字节跳动 TNS 部门被一锅端。在这个疫情还没有被宣布结束的时期,很多公司都会有这种裁员的动作,俗称组织架构优化,很多小公司都倒闭了,大公司也不得不想办法控制成本,那么这个时候倒霉的也只能是普通的打工人了。 没有一劳永逸的开始;也没有无法拯救的结束。人生中,你需要把握的是:该开始的,要义无反顾地开始;该结束的,就干净利落地结束。
3442 0
网传字节跳动 TNS 部门一锅端,转岗“难于上青天”
|
机器学习/深度学习 人工智能 自然语言处理
支付宝资深专家王维强:服务12亿用户的支付宝需要怎样的安全人才?
现在,支付宝安全团队正在招募应届生人才,欢迎投递简历。
3432 0
支付宝资深专家王维强:服务12亿用户的支付宝需要怎样的安全人才?