Android常用布局之TableLayout(表格布局)

概述:

TableLayout(即表格布局)

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

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

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

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

设置可被隐藏的列,如:android:collapseColumns=~0”, 表示第1列可被隐藏


对控件的属性有

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

实例

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".TableLayoutActivity">
    <!--    整体采用表格布局的方式-->
    <TextView
        android:id="@+id/text1"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:text="表格布局"
        android:gravity="center"
        android:textSize="50dp"
        />
<!--    默认一个控件为一行-->
    <Button
        android:layout_height="wrap_content"
        android:background="@color/teal_700">
    </Button>
    <Button
        android:layout_height="wrap_content"
        android:background="@color/purple_200">
    </Button>
    <TextView
        android:id="@+id/text2"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:text="TableRow"
        android:gravity="center"
        android:textSize="30dp"
        />
    <TableLayout
        android:layout_width="wrap_content"
        android:layout_height="400px"
        android:stretchColumns="2"
        >
    <TableRow>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="0"
            android:text="1"
            ></Button>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="1"
            android:text="2"
            ></Button>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="2"
            android:text="3"
            ></Button>
    </TableRow>
    <TableRow>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="1"
            android:text="4"
            ></Button>
    </TableRow>
    <TableRow>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="2"
                android:text="5"
                ></Button>
    </TableRow>
    </TableLayout>
    <TextView
        android:id="@+id/text3"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:text="隐藏第一列"
        android:gravity="center"
        android:textSize="30dp"
        />
    <TableLayout
        android:layout_width="wrap_content"
        android:layout_height="400px"
        android:collapseColumns="0"
        android:stretchColumns="2"
        >
        <TableRow>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="0"
                android:text="1"
                ></Button>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="1"
                android:text="2"
                ></Button>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="2"
                android:text="3"
                ></Button>
        </TableRow>
        <TableRow>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="1"
                android:text="4"
                ></Button>
        </TableRow>
        <TableRow>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="2"
                android:text="5"
                ></Button>
        </TableRow>
    </TableLayout>
</TableLayout>

实现效果:

 表格布局用的不是很多,但是我们也需要学习!

猜你喜欢

转载自blog.csdn.net/qq_62277763/article/details/128560369
今日推荐