Android学习|布局——TableLayout 表格布局

sxwgzx23 / 2023-05-10 / 原文

一、概述
TableLayout :即表格布局。

当TableLayout下面写控件、则控件占据一行的大小。(自适应一行,不留空白)

但是,想要多个组件占据一行,则配合TableRow实现

 


如下,设置三个button,其宽度为match_parent、按道应该不占据一行,而却一个button占了一整行

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

    <Button
    android:layout_width="match_parent"/>
    
    <Button
        android:layout_width="match_parent"/>
    
    <Button
        android:layout_width="match_parent"/>
    

</TableLayout>

添加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">

    <TableRow>
        <Button
            android:layout_width="match_parent"
            android:text="第一列"/>

        <Button
            android:layout_width="match_parent"
            android:text="第二列"/> 
    </TableRow>

    <TableRow>
        <Button
            android:layout_width="match_parent"
            android:text="第一列"/>

        <Button
            android:layout_width="match_parent"
            android:text="第二列"/>

        <Button
            android:layout_width="match_parent"
            android:text="第三列"/>

        <Button
            android:layout_width="match_parent"
            android:text="第四列"/>

        <Button
            android:layout_width="match_parent"
            android:text="第五列"/>
    </TableRow>


    <TableRow>
        <Button
            android:layout_width="match_parent"
            android:text="第一列"/>
    </TableRow>


</TableLayout>