android表格布局TableLayout

本文详细介绍了Android中的TableLayout组件,包括其基本使用方法及表格单元格的三种属性:Shrinkable、Stretchable 和 Collapsed。此外还提供了XML属性的示例代码,展示了如何设置表格的收缩、拉伸和隐藏列。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

TableLayout由所表格布局采用行列的形式来管理UI组件,TableLayout不需要明确声明包含多少行和列,而是通过添加TableRow,其他组件来控制表格的行列的。

向表格中添加TableRow和其他组件,该表格就增加一行,该组件占有一行。

表格的单元格常用三种属性:

Shrinkable:某列宽度可以被收缩,以保证适应父容器的宽度

Stretchable:某列宽度可以被拉伸,适应父容器的空间

Collapsed:该列的所有单元格会隐藏

XML Attributes
Attribute Name Related Method Description
android:collapseColumns setColumnCollapsed(int,boolean) The zero-based index of the columns to collapse. 
android:shrinkColumns setShrinkAllColumns(boolean) The zero-based index of the columns to shrink. 
android:stretchColumns setStretchAllColumns(boolean) The zero-based index of the columns to stretch. 

调用setColumnCollapsed(int,boolean)等方法的时候,多个列序号之间用逗号隔开

demo代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<TableLayout android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:shrinkColumns="1"
		android:stretchColumns="2">
		<Button android:layout_width="wrap_content"
			android:layout_height="wrap_content" android:text="第一个表格" />
	</TableLayout>

	<TableLayout android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:collapseColumns="2">
			<Button android:layout_width="wrap_content"
			android:layout_height="wrap_content" android:text="第二个表格" />
		<TableRow>
			<Button android:layout_width="wrap_content"
				android:layout_height="wrap_content" android:text="1" />
			<Button android:layout_width="wrap_content"
				android:layout_height="wrap_content" android:text="2" />
			<Button android:layout_width="wrap_content"
				android:layout_height="wrap_content" android:text="3" />
		</TableRow>
	</TableLayout>
	
		<TableLayout android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:stretchColumns="0,1,2">
			<Button android:layout_width="wrap_content"
			android:layout_height="wrap_content" android:text="第三个表格" />
		<TableRow>
			<Button android:layout_width="wrap_content"
				android:layout_height="wrap_content" android:text="1" />
			<Button android:layout_width="wrap_content"
				android:layout_height="wrap_content" android:text="2" />
			<Button android:layout_width="wrap_content"
				android:layout_height="wrap_content" android:text="3" />
		</TableRow>
	</TableLayout>
</LinearLayout>

 效果:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值