[android]笔记5-TableLayout

本文详细介绍了Android中的表格布局(TableLayout),包括其如何通过TableRow组件管理行和列,以及如何使用属性如android:shrinkColumns, android:stretchColumns, 和 android:collapseColumns 来控制列的行为。此外还提供了具体的XML配置实例。

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

表格布局由TableLayout所代表,TableLayout继承了LinearLayout,因此它的本质依然是线性布局器。表格布局采用行,列的形式来管理UI组件,TableLayout并不需要明确滴滴声明包含多少行,多少列,而是通过添加TabelRow,其他组件来控制表格的行数和列数。
每次向TableLayout中添加一个TableRow,该TableRow就是一个表格行,tableRow也是容器,因此它也可以不断地添加其他组件,每添加一个子组件该表格就增加一列。
如果直接向TableLayout中添加组件,那么这个组件直接占用一行
在表格布局中,列的矿都由该列中最宽的那个单元格决定,整个表格布局的宽度则取决于父容器的宽度(默认总是占满父容器本身)。
在表格布局中,列的宽度由该列中最宽的那个单元格决定,整个表格布局的宽度则取决于父容器的宽度(默认总是占满父容器本身)
TableLayout属性:
  android:collapseColumns:将TableLayout里面指定的列隐藏,若有多列需要隐藏,请用逗号将需要隐藏的列序号隔开。
  android:stretchColumns:设置指定的列为可伸展的列,以填满剩下的多余空白空间,若有多列需要设置为可伸展,请用逗号将需要伸展的列序号隔开。
  android:shrinkColumns:设置指定的列为可收缩的列。当可收缩的列太宽(内容过多)不会被挤出屏幕。当需要设置多列为可收缩时,将列序号用逗号隔开。
  

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.dezai.tablelayouttest.MainActivity">

    <TableLayout android:id="@+id/TableLayout01"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:shrinkColumns="1"
        android:stretchColumns="2">
        <Button android:id="@+id/ok1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="独占一行的按钮"/>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
            <Button android:id="@+id/ok2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="普通按钮"/>
            <Button android:id="@+id/ok3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="收缩按钮"/>
            <Button android:id="@+id/ok4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="拉伸按钮"/>
        </TableRow>

    </TableLayout>

    <TableLayout android:id="@+id/TableLayout02"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:collapseColumns="1">

        <Button android:id="@+id/ok9"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="独占一行的按钮"/>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
            <Button android:id="@+id/ok10"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="普通按钮1"/>
            <Button android:id="@+id/ok11"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="普通按钮2"/>
            <Button android:id="@+id/ok12"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="普通按钮3"/>

        </TableRow>
    </TableLayout>
    <TableLayout android:id="@+id/TableLayout03"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:stretchColumns="1,2">

        <Button android:id="@+id/ok13"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="独占一行的按钮"/>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
            <Button android:id="@+id/ok14"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="普通按钮"/>
            <Button android:id="@+id/ok15"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="拉伸按钮"/>
            <Button android:id="@+id/ok16"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="拉伸按钮"/>
        </TableRow>
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
            <Button android:id="@+id/ok17"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="普通按钮"/>
            <Button android:id="@+id/ok18"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="拉伸按钮"/>

        </TableRow>
    </TableLayout>
</LinearLayout>

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

厦门德仔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值