当前位置: 首页 > news >正文

Android 基础知识4-2.8 TableLayout(表格布局)详解

一、TableLayout的概述

        表格布局是以行数和列数来确定位置进行排列。就像一间教室,确定好行数与列数就能让同学有序入座。

注意:我们需要先添加<TableRow容器,每添加一个就会多一行,然后再往<TableRow容器中添加其它组件。

二、TableLayout的属性

    2.1 、TableLayout(表格布局)的样式,就像是一张表格。每个TableLayout,都由多个TableRow组成,每个TableRow就是一行,有几个TableRow就有几行。TableLayout不会显示行号和列号,也没有分割线,其行数和列数都可以进行操作。
        下面是 3 (行) x 3(列) 的TableLayout基本使用,其xml布局文件table_layout.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><TableRow><Buttonandroid:id="@+id/button01"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="按钮1" /><Buttonandroid:id="@+id/button02"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="按钮2" /><Buttonandroid:id="@+id/button03"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="按钮3" /></TableRow><TableRow><Buttonandroid:id="@+id/button04"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="按钮4" /><Buttonandroid:id="@+id/button05"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="按钮5" /><Buttonandroid:id="@+id/button06"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="按钮6" /></TableRow><TableRow><Buttonandroid:id="@+id/button07"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="按钮7" /><Buttonandroid:id="@+id/button08"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="按钮8" /><Buttonandroid:id="@+id/button09"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="按钮9" /></TableRow>
</TableLayout>

效果图:

 2.2 、TableLayout的android:shrinkColumns属性,当TableRow里边的空间布满布局的时候,指定列自动延伸以填充可用部分。当TableRow里边的控件还没有布满布局时,android:shrinkColumns不起作用。
下面的布局文件table_layout2.xml,演示了android:shrinkColumns属性的使用:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:shrinkColumns="2"><TableRow><Buttonandroid:id="@+id/button01"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="按钮1" /><!-- android:text="按钮1AAAAAAAAAAAAAAA" --><Buttonandroid:id="@+id/button02"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="按钮2" /><!-- android:text="按钮2AAAAAAAAAAAAAAA" --><Buttonandroid:id="@+id/button03"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="按钮3AAAAAAAAAAAAAAA" /></TableRow><TableRow><Buttonandroid:id="@+id/button04"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="按钮4" /><Buttonandroid:id="@+id/button05"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="按钮5" /><Buttonandroid:id="@+id/button06"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="按钮6" /></TableRow><TableRow><Buttonandroid:id="@+id/button07"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="按钮7" /><Buttonandroid:id="@+id/button08"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="按钮8" /><Buttonandroid:id="@+id/button09"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="按钮9" /></TableRow>
</TableLayout>

效果图:

         从上面的实际效果图片可以看到,当TableLayout设置了android:shrinkColumns属性,则在TableRow中的控件如果超长的话,设置指定的列为可收缩的列。当可收缩的列太宽(内容过多)不会被挤出屏幕。当需要设置多列为可收缩时,将列序号用逗号隔开。 

 2.3 、下面的布局文件table_layout3.xml,演示了没有设置android:shrinkColumns属性,则在TableRow中的控件超长,也不会自动延伸以填充可用部分。

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><TableRow><Buttonandroid:id="@+id/button01"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="按钮1" /><!-- android:text="按钮1AAAAAAAAAAAAAAA" --><Buttonandroid:id="@+id/button02"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="按钮2AAAAAAAAAAAAAAA" /><!-- android:text="按钮2" --><Buttonandroid:id="@+id/button03"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="按钮3AAAAAAAAAAAAAAA" /></TableRow><TableRow><Buttonandroid:id="@+id/button04"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="按钮4" /><Buttonandroid:id="@+id/button05"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="按钮5" /><Buttonandroid:id="@+id/button06"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="按钮6" /></TableRow><TableRow><Buttonandroid:id="@+id/button07"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="按钮7" /><Buttonandroid:id="@+id/button08"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="按钮8" /><Buttonandroid:id="@+id/button09"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="按钮9" /></TableRow>
</TableLayout>

效果图:

 2.4、TableLayout的android:stretchColumns属性,用于指定列对空白部分进行填充。
下面的布局文件table_layout4.xml,演示了android:stretchColumns属性的使用:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:stretchColumns="1"><TableRow><Buttonandroid:id="@+id/button01"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="按钮1"/><Buttonandroid:id="@+id/button02"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="按钮2"/><!-- android:text="按钮2" --><Buttonandroid:id="@+id/button03"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="按钮3"/></TableRow><TableRow><Buttonandroid:id="@+id/button04"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="按钮4"/><Buttonandroid:id="@+id/button05"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="按钮5"/><Buttonandroid:id="@+id/button06"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="按钮6"/></TableRow><TableRow><Buttonandroid:id="@+id/button07"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="按钮7"/><Buttonandroid:id="@+id/button08"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="按钮8"/><Buttonandroid:id="@+id/button09"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="按钮9"/></TableRow>
</TableLayout>

效果图:

 2.5、collapseColumns(隐藏列)

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

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:stretchColumns="1"><TableLayoutandroid: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>
</TableLayout>

效果图:

三、使用实例

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

流程解析:

(1).调用gravity属性,设置为center_vertical,让布局里面的组件在竖直方向上居中
(2).将TableLayout中的第一和第四列设置为可拉伸
(3).在每个TableRow中添加两个TextView,用于拉伸填满该行,这样可以让表格水平居中
android:stretchColumns=”0,3” 设置为0.3,是为了让两边都充满,那么中间部分就可以居中了

 

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/TableLayout1"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#FFFFFF"android:gravity="center_vertical"android:stretchColumns="0,3"><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="300dp" /></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="300dp" /></TableRow><TableRow><Buttonandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="登陆" /><Buttonandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="退出" /></TableRow></TableLayout>

效果图:

相关文章:

Android 基础知识4-2.8 TableLayout(表格布局)详解

一、TableLayout的概述 表格布局是以行数和列数来确定位置进行排列。就像一间教室&#xff0c;确定好行数与列数就能让同学有序入座。 注意&#xff1a;我们需要先添加<TableRow容器&#xff0c;每添加一个就会多一行&#xff0c;然后再往<TableRow容器中添加其它组件。…...

SQL代码编码原则和规范

目录1、先了解MySQL的执行过程2、数据库常见规范3、所有表必须使用Innodb存储引擎4、每个Innodb表必须有个主键5、数据库和表的字符集统一使用UTF86、查询SQL尽量不要使用select *&#xff0c;而是具体字段7、避免在where子句中使用 or 来连接条件8、尽量使用数值替代字符串类型…...

【博客627】gobgp服务无损变更:graceful restart特性

gobgp服务无损变更&#xff1a;graceful restart特性 场景 当我们的bgp网关在对外宣告bgp路由的时候&#xff0c;如果我们网关有新的特性要发布&#xff0c;那么此时如果把网关停止再启动新版本&#xff0c;此时bgp路由会有短暂撤回再播出的过程&#xff0c;会有网络抖动 期待…...

一起学 pixijs(1):常见图形的绘制

大家好&#xff0c;我是前端西瓜哥。 pixijs 是一个强大的 Web Canvas 2D 库&#xff0c;以其强大性能而著称。其底层使用了 WebGL 实现了硬件加速&#xff0c;当然如果不支持的话&#xff0c;也能回退为 Canvas。 本文使用的 pixijs 版本为 7.1.2。 Application Applicati…...

2023年PMP考试教材有哪些?(含pmp资料)

PMP考试教材是《PMBOK指南》&#xff0c;但这次的考试因为大纲的更新&#xff0c;而需要另外的敏捷书籍来备考。且官方发了通知&#xff0c;3、5月还是第六版指南&#xff0c;8月及8月之后&#xff0c;使用第七版教材。 新版考纲将专注于以下三个新领域: 人 – 强调与有效领导项…...

centos7防火墙工具firewall-cmd使用

centos7防火墙工具firewall-cmd使用防火墙概述centos7防火墙工具firewall-cmd使用介绍firewalld的基本使用服务管理工具相关指令配置firewalld-cmd防火墙概述 防火墙是可以帮助计算机在内部网络和外部网络之间构建一道相对隔绝的保护屏障&#xff0c;从而保护数据信息的一种技…...

js html过滤所有标签格式并清除所有nbsp;

var odiv document.getElementsByTagName("*"); for(var i 0; i<odiv.length; i){ if(odiv[i].className newDetail){ let obj odiv[i].childNodes[3]; let oldHtml odiv[i].childNodes[3].innerText;//获取html中不带标签内容 //console.log(odiv[i].childN…...

「技术选型」深度学习软件如何选择?

深度学习(DL, Deep Learning)是机器学习(ML, Machine Learning)领域中一个新的研究方向&#xff0c;它被引入机器学习使其更接近于最初的目标——人工智能(AI, Artificial Intelligence)。 深度学习是学习样本数据的内在规律和表示层次&#xff0c;这些学习过程中获得的信息对…...

加油站会员管理小程序实战开发教程13

我们上一篇讲解了会员注册的功能,本篇我们介绍一下会员开卡的功能。 会员注册之后,可以进行开卡的动作。一个会员可以有多张会员卡,在微搭中用来描述这种一对多的关系的,我们用关联关系来表达。 登录微搭的控制台,点击数据模型,点击新建数据模型 输入数据源的名称会员卡…...

Go语言Web入门之浅谈Gin框架

Gin框架Gin简介第一个Gin示例HelloworldRESTful APIGin返回数据的几种格式Gin 获取参数HTTP重定向Gin路由&路由组Gin框架当中的中间件Gin简介 Gin 是一个用 Go (Golang) 编写的 web 框架。它是一个类似于 martini 但拥有更好性能的 API 框架&#xff0c;由于 httprouter&a…...

《MySQL学习》 MySQL优化器选择如何选择索引

一.优化器的选择逻辑 建表语句 CREATE TABLE t (id int(11) NOT NULL AUTO_INCREMENT,a int(11) DEFAULT NULL,b int(11) DEFAULT NULL,PRIMARY KEY (id),KEY a (a),KEY b (b) ) ENGINEInnoDB;往表中插入10W条数据 delimiter ;; create procedure idata() begindeclare i in…...

uniapp 悬浮窗(应用内、无需授权) Ba-FloatWindow2

简介&#xff08;下载地址&#xff09; Ba-FloatWindow2 是一款应用内并且无需授权的悬浮窗插件。支持多种拖动&#xff1b;自定义位置、大小&#xff1b;支持动态修改。 支持自动定义起始位置支持自定义悬浮窗大小支持贴边显示支持多种拖动方效果&#xff1a;不可拖动、任意…...

MMKV与mmap:全方位解析

概述 MMKV 是基于 mmap 内存映射的移动端通用 key-value 组件&#xff0c;底层序列化/反序列化使用 protobuf 实现&#xff0c;性能高&#xff0c;稳定性强。从 2015 年中至今&#xff0c;在 iOS 微信上使用已有近 3 年&#xff0c;其性能和稳定性经过了时间的验证。近期已移植…...

【信息系统项目管理师】项目管理十大知识领域记忆敲出(整体范围进度)

【信息系统项目管理师】项目管理十大知识领域记忆敲出&#xff08;整体范围进度&#xff09; 【信息系统项目管理师】项目管理十大知识领域记忆敲出&#xff08;整体范围进度&#xff09;【信息系统项目管理师】项目管理十大知识领域记忆敲出&#xff08;整体范围进度&#xff…...

一起学 pixijs(3):Sprite

大家好&#xff0c;我是前端西瓜哥。今天来学习 pixijs 的 Sprite。 Sprite pixijs 的 Sprite 类用于将一些纹理&#xff08;Texture&#xff09;渲染到屏幕上。 Sprite 直译为 “精灵”&#xff0c;是游戏开发中常见的术语&#xff0c;就是将一个角色的多个动作放到一个图片…...

深入讲解Kubernetes架构-垃圾收集

垃圾收集&#xff08;Garbage Collection&#xff09;是 Kubernetes 用于清理集群资源的各种机制的统称。 垃圾收集允许系统清理如下资源&#xff1a;终止的 Pod已完成的 Job不再存在属主引用的对象未使用的容器和容器镜像动态制备的、StorageClass 回收策略为 Delete 的 PV 卷…...

Flink03: 集群安装部署

Flink支持多种安装部署方式 StandaloneON YARNMesos、Kubernetes、AWS… 这些安装方式我们主要讲一下standalone和on yarn。 如果是一个独立环境的话&#xff0c;可能会用到standalone集群模式。 在生产环境下一般还是用on yarn 这种模式比较多&#xff0c;因为这样可以综合利…...

OCR项目实战(一):手写汉语拼音识别(Pytorch版)

✨写在前面&#xff1a;强烈推荐给大家一个优秀的人工智能学习网站&#xff0c;内容包括人工智能基础、机器学习、深度学习神经网络等&#xff0c;详细介绍各部分概念及实战教程&#xff0c;非常适合人工智能领域初学者及研究者学习。➡️点击跳转到网站。 &#x1f4dd;OCR专栏…...

【js】export default也在影响项目性能呢

这里写目录标题介绍先说结论分析解决介绍 无意间看到一个关于export与exprot default对比的话题&#xff0c; 于是对二者关于性能方面&#xff0c;有了想法&#xff0c;二者的区别&#xff0c;仅仅是在于写法吗&#xff1f; 于是&#xff0c;有了下面的测试。 先说结论 太长…...

《软件安全》 彭国军 阅读总结

对于本书&#xff0c;小编本意是对其讲述的内容&#xff0c;分点进行笔记的整理&#xff0c;后来学习以后&#xff0c;发现&#xff0c;这本书应该不算是一本技术提升类的书籍&#xff0c;更像是一本领域拓展和知识科普类书籍&#xff0c;所讲知识广泛&#xff0c;但是较少实践…...

eNSP-Cloud(实现本地电脑与eNSP内设备之间通信)

说明&#xff1a; 想象一下&#xff0c;你正在用eNSP搭建一个虚拟的网络世界&#xff0c;里面有虚拟的路由器、交换机、电脑&#xff08;PC&#xff09;等等。这些设备都在你的电脑里面“运行”&#xff0c;它们之间可以互相通信&#xff0c;就像一个封闭的小王国。 但是&#…...

多模态2025:技术路线“神仙打架”,视频生成冲上云霄

文&#xff5c;魏琳华 编&#xff5c;王一粟 一场大会&#xff0c;聚集了中国多模态大模型的“半壁江山”。 智源大会2025为期两天的论坛中&#xff0c;汇集了学界、创业公司和大厂等三方的热门选手&#xff0c;关于多模态的集中讨论达到了前所未有的热度。其中&#xff0c;…...

Auto-Coder使用GPT-4o完成:在用TabPFN这个模型构建一个预测未来3天涨跌的分类任务

通过akshare库&#xff0c;获取股票数据&#xff0c;并生成TabPFN这个模型 可以识别、处理的格式&#xff0c;写一个完整的预处理示例&#xff0c;并构建一个预测未来 3 天股价涨跌的分类任务 用TabPFN这个模型构建一个预测未来 3 天股价涨跌的分类任务&#xff0c;进行预测并输…...

Rust 异步编程

Rust 异步编程 引言 Rust 是一种系统编程语言,以其高性能、安全性以及零成本抽象而著称。在多核处理器成为主流的今天,异步编程成为了一种提高应用性能、优化资源利用的有效手段。本文将深入探讨 Rust 异步编程的核心概念、常用库以及最佳实践。 异步编程基础 什么是异步…...

今日学习:Spring线程池|并发修改异常|链路丢失|登录续期|VIP过期策略|数值类缓存

文章目录 优雅版线程池ThreadPoolTaskExecutor和ThreadPoolTaskExecutor的装饰器并发修改异常并发修改异常简介实现机制设计原因及意义 使用线程池造成的链路丢失问题线程池导致的链路丢失问题发生原因 常见解决方法更好的解决方法设计精妙之处 登录续期登录续期常见实现方式特…...

Kafka入门-生产者

生产者 生产者发送流程&#xff1a; 延迟时间为0ms时&#xff0c;也就意味着每当有数据就会直接发送 异步发送API 异步发送和同步发送的不同在于&#xff1a;异步发送不需要等待结果&#xff0c;同步发送必须等待结果才能进行下一步发送。 普通异步发送 首先导入所需的k…...

4. TypeScript 类型推断与类型组合

一、类型推断 (一) 什么是类型推断 TypeScript 的类型推断会根据变量、函数返回值、对象和数组的赋值和使用方式&#xff0c;自动确定它们的类型。 这一特性减少了显式类型注解的需要&#xff0c;在保持类型安全的同时简化了代码。通过分析上下文和初始值&#xff0c;TypeSc…...

QT开发技术【ffmpeg + QAudioOutput】音乐播放器

一、 介绍 使用ffmpeg 4.2.2 在数字化浪潮席卷全球的当下&#xff0c;音视频内容犹如璀璨繁星&#xff0c;点亮了人们的生活与工作。从短视频平台上令人捧腹的搞笑视频&#xff0c;到在线课堂中知识渊博的专家授课&#xff0c;再到影视平台上扣人心弦的高清大片&#xff0c;音…...

Java详解LeetCode 热题 100(26):LeetCode 142. 环形链表 II(Linked List Cycle II)详解

文章目录 1. 题目描述1.1 链表节点定义 2. 理解题目2.1 问题可视化2.2 核心挑战 3. 解法一&#xff1a;HashSet 标记访问法3.1 算法思路3.2 Java代码实现3.3 详细执行过程演示3.4 执行结果示例3.5 复杂度分析3.6 优缺点分析 4. 解法二&#xff1a;Floyd 快慢指针法&#xff08;…...

[特殊字符] 手撸 Redis 互斥锁那些坑

&#x1f4d6; 手撸 Redis 互斥锁那些坑 最近搞业务遇到高并发下同一个 key 的互斥操作&#xff0c;想实现分布式环境下的互斥锁。于是私下顺手手撸了个基于 Redis 的简单互斥锁&#xff0c;也顺便跟 Redisson 的 RLock 机制对比了下&#xff0c;记录一波&#xff0c;别踩我踩过…...