(六)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

相关文章

Java Web实现类似Excel表格锁定功能实战教程

《JavaWeb实现类似Excel表格锁定功能实战教程》本文将详细介绍通过创建特定div元素并利用CSS布局和JavaScript事件监听来实现类似Excel的锁定行和列效果的方法,感兴趣的朋友跟随... 目录1. 模拟Excel表格锁定功能2. 创建3个div元素实现表格锁定2.1 div元素布局设计2.

Android DataBinding 与 MVVM使用详解

《AndroidDataBinding与MVVM使用详解》本文介绍AndroidDataBinding库,其通过绑定UI组件与数据源实现自动更新,支持双向绑定和逻辑运算,减少模板代码,结合MV... 目录一、DataBinding 核心概念二、配置与基础使用1. 启用 DataBinding 2. 基础布局

Android ViewBinding使用流程

《AndroidViewBinding使用流程》AndroidViewBinding是Jetpack组件,替代findViewById,提供类型安全、空安全和编译时检查,代码简洁且性能优化,相比Da... 目录一、核心概念二、ViewBinding优点三、使用流程1. 启用 ViewBinding (模块级

Python实现精准提取 PDF中的文本,表格与图片

《Python实现精准提取PDF中的文本,表格与图片》在实际的系统开发中,处理PDF文件不仅限于读取整页文本,还有提取文档中的表格数据,图片或特定区域的内容,下面我们来看看如何使用Python实... 目录安装 python 库提取 PDF 文本内容:获取整页文本与指定区域内容获取页面上的所有文本内容获取

Android学习总结之Java和kotlin区别超详细分析

《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio

使用C#删除Excel表格中的重复行数据的代码详解

《使用C#删除Excel表格中的重复行数据的代码详解》重复行是指在Excel表格中完全相同的多行数据,删除这些重复行至关重要,因为它们不仅会干扰数据分析,还可能导致错误的决策和结论,所以本文给大家介绍... 目录简介使用工具C# 删除Excel工作表中的重复行语法工作原理实现代码C# 删除指定Excel单元

使用Python实现网页表格转换为markdown

《使用Python实现网页表格转换为markdown》在日常工作中,我们经常需要从网页上复制表格数据,并将其转换成Markdown格式,本文将使用Python编写一个网页表格转Markdown工具,需... 在日常工作中,我们经常需要从网页上复制表格数据,并将其转换成Markdown格式,以便在文档、邮件或

Python实现pdf电子发票信息提取到excel表格

《Python实现pdf电子发票信息提取到excel表格》这篇文章主要为大家详细介绍了如何使用Python实现pdf电子发票信息提取并保存到excel表格,文中的示例代码讲解详细,感兴趣的小伙伴可以跟... 目录应用场景详细代码步骤总结优化应用场景电子发票信息提取系统主要应用于以下场景:企业财务部门:需

Python实现获取带合并单元格的表格数据

《Python实现获取带合并单元格的表格数据》由于在日常运维中经常出现一些合并单元格的表格,如果要获取数据比较麻烦,所以本文我们就来聊聊如何使用Python实现获取带合并单元格的表格数据吧... 由于在日常运维中经常出现一些合并单元格的表格,如果要获取数据比较麻烦,现将将封装成类,并通过调用list_exc

CSS3 布局样式及其应用举例

《CSS3布局样式及其应用举例》CSS3的布局特性为前端开发者提供了无限可能,无论是Flexbox的一维布局还是Grid的二维布局,它们都能够帮助开发者以更清晰、简洁的方式实现复杂的网页布局,本文给... 目录深入探讨 css3 布局样式及其应用引言一、CSS布局的历史与发展1.1 早期布局的局限性1.2