【5】Android 布局 - TableLayout

前言

学过 HTML 的小伙伴应该知道,HTML中可以使用 table 标签 构造表格。在 Android 中也允许我们可以通过表格的格式对组件进行排序。

Tablelayout 类 以行和列的形式对控件进行管理。

默认情况下,每个子控件占据一行。若想让多个控件布局在同一行,则需要通过以下代码实现

<TableRow>
    /*同行控件xml代码*/
</TableRow>

列数则由于 TableLayout 中组件的个数决定(TableRow 及其所包含的组件共占一行)。

注意

TableRow layout_width 属性,默认是 match_parent 的,且设置为其他值无法生效。

layout_height 默认值也是  wrap_conten ,我们可以自行设置其大小。

相关属性

注意

以上这三个属性的列号都是从0开始算的

shrinkColunmns = "0,2"

对应的是第一列和第三列。若需所有列都生效,则用 "*" 即可。

除了这三个常用属性,还有两个属性,但是我在 TableLayout 的官方API中没有找到
android:layout_column
android:layout_span

示例

<?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="wrap_content"
    android:collapseColumns="2"
    android:stretchColumns="1"
    android:layout_columnSpan="0">
    <!--隐藏第三列(按键3)-->
    <!--拉伸第二列(按键2)-->
    <!--压缩第一列(按键1)-->
    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="200dp"
            android:background="#0fff0f"
            android:text="1" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#2196F3"
            android:text="2" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#FF9800"
            android:text="3" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#404040"
            android:text="4" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />
    </TableRow>



</TableLayout>

效果

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值