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

Android学习之表格布局

表格布局(Tablelayout)
表格布局与常见的表格类似,它以行、列的形式来管理放入其中的视图,表格布局使用<TableLayout>标记定义,在布局中,可以添加多个<TableRow>标记,每个<TableRow>标记占用一行,由于<TableRow>标记也是容器,所以在该标记中还可以添加其他视图,在<TableRow>标记中,每添加一个视图,表格就会增加一列。在表格布局中,列可以被隐藏,也可以被设置为伸展,从而填充可利用的屏幕空间,也可设置为强制收缩,直到表格匹配屏幕大小。
如果在表格布局<TableLayout>中直接添加视图,那么这个视图将独占一行。
表格布局TableLayout继承了线性布局LinearLayout,所以表格布局完全支持线性布局所支持的全部XML属性。

TableLayout的列数
等于含有最多子控件的TableRow的列数。
如第一(行)TableRow含2个子控件,第二(行)TableRow含3个,第三(行)TableRow含4个,那么这个表格布局的列数就是4列。

TableLayout可设置的属性
表格布局可以设置的属性有两种:全局属性、单元格属性。
全局属性(列属性): 全局属性有三个属性
Android:stretchColumns 设置可伸展的列,让列填满表格剩余宽度。该列可以向行方向伸展,最多可占据一整行。
Android:shrinkColumns 设置可收缩的列,内容太长自动换行 / 缩小。(当该列子控件里的内容太多,行内显示不完的时候会向列的方向显示内容)。
Android:collapseColumns 设置要隐藏的列。
例如:
Android:stretchColumns=”0″ 第0列可伸展

Android:stretchColumns=”*″ =*伸展全部列

Android:shrinkColumns=”1,2″ 第1,2列皆可收缩
Android:collapseColumns=”1″ 隐藏第一行

单元格属性:有两个
Android:layout_column 指定该单元格在第几列显示
Android:layout_span 指定该单元格占据的列数(如果我们在使用中没有指定,那么默认值将为1)
例如:
Android:layout_column=”1″ 该控件在第1列
Android:layout_span=”2″ 该控件占了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"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第一个按钮" /> <TableRow android:layout_marginTop="50dp"> <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="第二个按钮" android:layout_span="2"/> </TableRow> <TableRow android:layout_marginTop="10dp" android:layout_marginStart="20dp"> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="第三个按钮" /> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="第四个按钮" /> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="第五个按钮" /> </TableRow> <TableRow> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:text="第6个按钮" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="15dp" android:text="第7个按钮" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第8个按钮" /> </TableRow> </TableLayout>

http://www.cnnetsun.cn/news/1333948.html

相关文章:

  • 08 Python 数据分析:学生画像匹配与相似度计算
  • 基于Spring Boot的濒危物种公益救助交流平台
  • 2026 答辩 PPT 效率革命:实测 10 款 AI 生成工具,Paperzz 凭「论文适配 + 全场景模板」登顶
  • 虚拟经济典狱长:软件测试视角下的NFT破产富豪监管
  • MySQL 索引下推(Index Condition Pushdown, ICP)机制详解
  • 扬帆深蓝,共赴未来——写在国际航海日
  • 打工人上班摸魚小說-第二十六章 北城、劳务市场与藏在人群里的眼睛
  • 打工人上班摸魚小說-第二十七章 逃跑、雨夜与地下通道的七分钟
  • 大厂朋友AI转型屡屡碰壁?揭秘AI产品经理正确入门路径,避开这些坑!
  • 百考通:AI赋能开题报告,让学术研究起步更高效
  • 315质量榜:为何这家企业成为玩具喷涂废气治理厂家优选?
  • 轨道交通部分线缆型号介绍
  • 2026年企业如何选对HR系统?
  • 苹果转安卓|3 种数据迁移方法,小白也能轻松搞定
  • 工厂老是临时插单怎么办?——从被动响应到有序管控的实践路径
  • 用本地 Chrome + DeepSeek 实现浏览器自动化:browser-use 实战与踩坑记录
  • 广州百度销售联系方式更新了吗
  • 上下文利用率
  • Java中的动态规划THREE——DP
  • 20260317_163145_SRC挖掘?看这篇就够了,保姆级教程带你飞!
  • 重新标注ImageNet!128万张图像,单标签变多标签!这个预训练模型让COCO暴涨4个点
  • skynet Monitor 线程详解
  • 2026笔记本Windows电源管理:硬盘休眠与PCIe链路
  • Python 实战:基于朴素贝叶斯的中文评价情感分析(好评 / 差评自动识别)| 附完整可运行代码
  • 一文详解Diffusion Policy
  • C++11中智能指针:shared_ptr的引用计数是线程安全的吗?
  • 不懂代码,我用AI编程给5岁女儿开发了个流光画板(带你一步一步设计一个属于自己的流光画板)
  • VScode快捷键
  • 小白从零开始勇闯人工智能:LangChain 入门指南(下)
  • AI时代的教育“外包”:中国家长将作业辅导交给机器