常见界面布局之TableLayout(表格布局)

1.什么是TableLayout(表格布局)

TableLayout (表格布局)采用行、列的形式来管理控件,它不需要明确声明包含多少行、多少列,面是通过在TableLayout布局中添加TableRow布局或控件来控制表格的行数,可以在TableRow 布局中添加控件来控制表格的列数。在XML布局文件中定义表格布局的基本语法格式如下:

<TableLayout xmlns:android-"http://schemas.android.com/apk/res/android" 
	属性-”属性值">
<TableRow>
UI控件
</TableRow>
UI控件
</TableLayout>

TableLayout继承自LinearLayout, 因此它完全支持LinearLayout所支持的属性,此外,它还有其他的常用属性。TableLayout布局的常用属性如表所示。
表 TableLayout 布局的常用属性

属性名称功能描述
android:stretchColumns设置可被拉伸的列。如:android:stretchColumns-~0”, 表示第1列可被拉伸
android:shrinkColumns设置可被收缩的列,如:android:shrinkColumns=“1. 2”, 表示2, 3列可收编
android:collapseColumns设置可被隐藏的列,如:android:collapseColumns=~0”, 表示第1列可被隐藏

TableLayout布局中的控件有两个常用属性android:layout_column与jandroid:layout_span, 分别用
于设置控件显示的位置、占据的行数,如表所示。
表 TableLayout 布局中控件的常用属性

属性名称功能描述
android:layout_column设置该控件显示的位置,如 android:layout_column-"l”表示在第2个位置显示
android:layout_span设置该控件占据儿行,默认为1行

在TableLayout(表格布局)中,列的宽度由该列中最宽的那个单元格(控件)决定,整个表格布局的宽度则取决于父容器的宽度。

  1. 创建程序

    创建一个名为TableLayout的应用程序,指定包名为cn.itcast.tablelayout.

  2. 放置界面控件

在activity_main.xml文件的TableLayout布局中放置3个TableRow布

局,在TableRow布局中添加不同数量的按钮。
在这里插入图片描述

<?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"

  //设置可被拉伸的列

    android:stretchColumns="2"
    >
    <TableRow>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

      //设置该控件显示的位置            

      android:layout_column="0"

            android:text="按钮1"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="1"
            android:text="按钮2"
            />
    </TableRow>
    <TableRow>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="1"
            android:text="按钮3"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="2"
            android:text="按钮4"
            />
    </TableRow>
    <TableRow>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="2"
            android:text="按钮5"
            />
    </TableRow>
</TableLayout>

在这里插入图片描述

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值