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

相关文章

C#实现将Excel表格转换为图片(JPG/ PNG)

《C#实现将Excel表格转换为图片(JPG/PNG)》Excel表格可能会因为不同设备或字体缺失等问题,导致格式错乱或数据显示异常,转换为图片后,能确保数据的排版等保持一致,下面我们看看如何使用C... 目录通过C# 转换Excel工作表到图片通过C# 转换指定单元格区域到图片知识扩展C# 将 Excel

Android Mainline基础简介

《AndroidMainline基础简介》AndroidMainline是通过模块化更新Android核心组件的框架,可能提高安全性,本文给大家介绍AndroidMainline基础简介,感兴趣的朋... 目录关键要点什么是 android Mainline?Android Mainline 的工作原理关键

如何解决idea的Module:‘:app‘platform‘android-32‘not found.问题

《如何解决idea的Module:‘:app‘platform‘android-32‘notfound.问题》:本文主要介绍如何解决idea的Module:‘:app‘platform‘andr... 目录idea的Module:‘:app‘pwww.chinasem.cnlatform‘android-32

前端CSS Grid 布局示例详解

《前端CSSGrid布局示例详解》CSSGrid是一种二维布局系统,可以同时控制行和列,相比Flex(一维布局),更适合用在整体页面布局或复杂模块结构中,:本文主要介绍前端CSSGri... 目录css Grid 布局详解(通俗易懂版)一、概述二、基础概念三、创建 Grid 容器四、定义网格行和列五、设置行

Android实现打开本地pdf文件的两种方式

《Android实现打开本地pdf文件的两种方式》在现代应用中,PDF格式因其跨平台、稳定性好、展示内容一致等特点,在Android平台上,如何高效地打开本地PDF文件,不仅关系到用户体验,也直接影响... 目录一、项目概述二、相关知识2.1 PDF文件基本概述2.2 android 文件访问与存储权限2.

MySQL 中查询 VARCHAR 类型 JSON 数据的问题记录

《MySQL中查询VARCHAR类型JSON数据的问题记录》在数据库设计中,有时我们会将JSON数据存储在VARCHAR或TEXT类型字段中,本文将详细介绍如何在MySQL中有效查询存储为V... 目录一、问题背景二、mysql jsON 函数2.1 常用 JSON 函数三、查询示例3.1 基本查询3.2

Android Studio 配置国内镜像源的实现步骤

《AndroidStudio配置国内镜像源的实现步骤》本文主要介绍了AndroidStudio配置国内镜像源的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、修改 hosts,解决 SDK 下载失败的问题二、修改 gradle 地址,解决 gradle

在Android平台上实现消息推送功能

《在Android平台上实现消息推送功能》随着移动互联网应用的飞速发展,消息推送已成为移动应用中不可或缺的功能,在Android平台上,实现消息推送涉及到服务端的消息发送、客户端的消息接收、通知渠道(... 目录一、项目概述二、相关知识介绍2.1 消息推送的基本原理2.2 Firebase Cloud Me

Pydantic中Optional 和Union类型的使用

《Pydantic中Optional和Union类型的使用》本文主要介绍了Pydantic中Optional和Union类型的使用,这两者在处理可选字段和多类型字段时尤为重要,文中通过示例代码介绍的... 目录简介Optional 类型Union 类型Optional 和 Union 的组合总结简介Pyd

Oracle数据库常见字段类型大全以及超详细解析

《Oracle数据库常见字段类型大全以及超详细解析》在Oracle数据库中查询特定表的字段个数通常需要使用SQL语句来完成,:本文主要介绍Oracle数据库常见字段类型大全以及超详细解析,文中通过... 目录前言一、字符类型(Character)1、CHAR:定长字符数据类型2、VARCHAR2:变长字符数