TableLayout初识初识

TableLayout表格布局

<?xml version="1.0" encoding="utf-8"?>
<TableLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_tablelayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.axnet.buju0711.Tablelayout">
    <!--表格布局-->
    <Button
        android:text="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    <Button
        android:text="2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    <Button
        android:text="3"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        />
</TableLayout>

这里写图片描述
layout_width默认设置match_parent,即使这是200dp也是match_parent
宽度是不可以自己设置的,但是高度是可以自己设置的


显然,一列多行并不能满足大家的需求
这时候就出现了

<TableRow>

把button123放入TableRow中
每个TableRow代表一行

<?xml version="1.0" encoding="utf-8"?>
<TableLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_tablelayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.axnet.buju0711.Tablelayout">
    <!--表格布局-->
    <TableRow>
        <Button
            android:text="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
        <Button
            android:text="2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
        <Button
            android:text="3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
    </TableRow>
</TableLayout>

这里写图片描述
加入TableRow以后,我们修改

 <Button
            android:text="1"
            android:layout_width="150dp"
            android:layout_height="150dp"
            />

这里写图片描述
**发现修改一个的宽度,一列的宽度都是变化的,

修改高度,只有修改的控件高度会变化**

android:stretchColumns拉伸
android:layout_span合并行

<?xml version="1.0" encoding="utf-8"?>
<TableLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_tablelayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.axnet.buju0711.Tablelayout">
    <!--表格布局-->
    <TableRow>
        <Button
            android:text="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_span="2"
            />
        <!--<Button-->
        <!--android:text="2"-->
        <!--android:layout_width="wrap_content"-->
        <!--android:layout_height="wrap_content"-->
        <!--/>-->
        <Button
            android:text="3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
    </TableRow>
    <TableRow>
        <Button
            android:text="4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
        <Button
            android:text="5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_span="2"
            />
        <!--<Button-->
        <!--android:text="6"-->
        <!--android:layout_width="wrap_content"-->
        <!--android:layout_height="wrap_content"-->
        <!--/>-->
    </TableRow>
    <TableRow>
        <Button
            android:text="7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
        <Button
            android:text="8"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
        <Button
            android:text="9"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
    </TableRow>
</TableLayout>

这里写图片描述
layout_span必须要有相对的参照物才能起作用

如何使每个横行占满屏幕
android:stretchColumns拉伸

//拉伸第三列
  android:stretchColumns="2"

这里写图片描述

 android:stretchColumns="*"
 拉伸全部列

这里写图片描述
———-*

如果之拉伸第二三列
android:stretchColumns=”1,2” 用英文逗号分开


如果想要控件居中或者占满整个一行单元格

 <TableRow>
        <Button
            android:text="10"
            android:layout_column="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
    </TableRow>
    <TableRow>
        <Button
            android:text="11"
            android:layout_span="3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
    </TableRow>

这里写图片描述


**TableLayout合并单元格只可以合并行
不可以合并列**

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值