(六)Android布局类型(表格布局TableLayout)

2024-03-17 03:20

本文主要是介绍(六)Android布局类型(表格布局TableLayout),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

表格布局(TableLayout),呈现行列方式,无法设置列,可以设置行,行数由TableRow对象个数决定。下图中有两个TableRow元素,所以,说明表格布局中有两行。

将内容填充到行中

第一行中,添加了四个按钮组件,在组件树中也能清晰看到,右侧效果图中,四个按钮水平排列。

看到右侧还有空白,于是再增加一个按钮

增加后,发现超出界面。

解决办法1:删除新增按钮,将最后一个按钮进行拉伸

属性1:android:stretchColumns

android:stretchColumns用来设置允许被拉伸的列的列序号(列序号从0开始计数)

解决办法2:保留新增按钮,将最后一个按钮进行压缩

属性2:android:shrinkColumns

android:shrinkColumns:设置允许被收缩的列的列序号(列序号从0开始计数)

属性3:android:collapseColumns

android:collapseColumns:设置被隐藏的列的序号(列序号从0开始计数)

假设列序号为1的列需要被隐藏起来(蓝色标注)

则可以增加该属性

代码:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"android:collapseColumns="1"><TableRow android:layout_width="match_parent"android:layout_height="wrap_content"><Button android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="0行0列"/><Button android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="0行1列"/><Button android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="0行2列"/><Button android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="0行3列"/></TableRow><TableRow android:layout_width="match_parent"android:layout_height="wrap_content"><Button android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="1行0列"/><Button android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="1行1列"/><Button android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="1行2列"/><Button android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="1行3列"/></TableRow>
</TableLayout>

效果:

 

从效果中可以看出,序列号为1的列被隐藏起来,后面的列依次往前移了位置

核心代码:在表格布局中,除了以上三个属性比较常用,还有一个常用功能:跨列

表格布局实现跨列案例 

目标效果图如下:第二行中,第一个位置占据了两列的效果

具体操作:在第2行第1列上设置跨列数为2,再将第2行第2列的代码注释掉。注释掉的原因是:因为位置已经被前面一个组件占用了。

整体代码:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><TableRow android:layout_width="match_parent"android:layout_height="wrap_content"><Button android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="0行0列"/><Button android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="0行1列"/><Button android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="0行2列"/><Button android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="0行3列"/></TableRow><TableRow android:layout_width="match_parent"android:layout_height="wrap_content"><Button android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_span="2"android:text="1行0列"/>
<!--        <Button android:layout_width="wrap_content"-->
<!--            android:layout_height="wrap_content"-->
<!--            android:text="1行1列"/>--><Button android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="1行2列"/><Button android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="1行3列"/></TableRow>
</TableLayout>

注意:表格布局无法实现跨行。

这篇关于(六)Android布局类型(表格布局TableLayout)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/817645

相关文章

Android协程高级用法大全

《Android协程高级用法大全》这篇文章给大家介绍Android协程高级用法大全,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友跟随小编一起学习吧... 目录1️⃣ 协程作用域(CoroutineScope)与生命周期绑定Activity/Fragment 中手

python中的显式声明类型参数使用方式

《python中的显式声明类型参数使用方式》文章探讨了Python3.10+版本中类型注解的使用,指出FastAPI官方示例强调显式声明参数类型,通过|操作符替代Union/Optional,可提升代... 目录背景python函数显式声明的类型汇总基本类型集合类型Optional and Union(py

MySQL中查询和展示LONGBLOB类型数据的技巧总结

《MySQL中查询和展示LONGBLOB类型数据的技巧总结》在MySQL中LONGBLOB是一种二进制大对象(BLOB)数据类型,用于存储大量的二进制数据,:本文主要介绍MySQL中查询和展示LO... 目录前言1. 查询 LONGBLOB 数据的大小2. 查询并展示 LONGBLOB 数据2.1 转换为十

C#使用Spire.XLS快速生成多表格Excel文件

《C#使用Spire.XLS快速生成多表格Excel文件》在日常开发中,我们经常需要将业务数据导出为结构清晰的Excel文件,本文将手把手教你使用Spire.XLS这个强大的.NET组件,只需几行C#... 目录一、Spire.XLS核心优势清单1.1 性能碾压:从3秒到0.5秒的质变1.2 批量操作的优雅

MyBatis的xml中字符串类型判空与非字符串类型判空处理方式(最新整理)

《MyBatis的xml中字符串类型判空与非字符串类型判空处理方式(最新整理)》本文给大家介绍MyBatis的xml中字符串类型判空与非字符串类型判空处理方式,本文给大家介绍的非常详细,对大家的学习或... 目录完整 Hutool 写法版本对比优化为什么status变成Long?为什么 price 没事?怎

Android 缓存日志Logcat导出与分析最佳实践

《Android缓存日志Logcat导出与分析最佳实践》本文全面介绍AndroidLogcat缓存日志的导出与分析方法,涵盖按进程、缓冲区类型及日志级别过滤,自动化工具使用,常见问题解决方案和最佳实... 目录android 缓存日志(Logcat)导出与分析全攻略为什么要导出缓存日志?按需过滤导出1. 按

C#之枚举类型与随机数详解

《C#之枚举类型与随机数详解》文章讲解了枚举类型的定义与使用方法,包括在main外部声明枚举,用于表示游戏状态和周几状态,枚举值默认从0开始递增,也可手动设置初始值以生成随机数... 目录枚举类型1.定义枚举类型(main外)2.使用生成随机数总结枚举类型1.定义枚举类型(main外)enum 类型名字

Python lambda函数(匿名函数)、参数类型与递归全解析

《Pythonlambda函数(匿名函数)、参数类型与递归全解析》本文详解Python中lambda匿名函数、灵活参数类型和递归函数三大进阶特性,分别介绍其定义、应用场景及注意事项,助力编写简洁高效... 目录一、lambda 匿名函数:简洁的单行函数1. lambda 的定义与基本用法2. lambda

C语言自定义类型之联合和枚举解读

《C语言自定义类型之联合和枚举解读》联合体共享内存,大小由最大成员决定,遵循对齐规则;枚举类型列举可能值,提升可读性和类型安全性,两者在C语言中用于优化内存和程序效率... 目录一、联合体1.1 联合体类型的声明1.2 联合体的特点1.2.1 特点11.2.2 特点21.2.3 特点31.3 联合体的大小1

MySQL 索引简介及常见的索引类型有哪些

《MySQL索引简介及常见的索引类型有哪些》MySQL索引是加速数据检索的特殊结构,用于存储列值与位置信息,常见的索引类型包括:主键索引、唯一索引、普通索引、复合索引、全文索引和空间索引等,本文介绍... 目录什么是 mysql 的索引?常见的索引类型有哪些?总结性回答详细解释1. MySQL 索引的概念2