天天看點

TableLayout布局常用屬性案例代碼

在TableLayout布局布局中加入按鈕,會占據一行,想要多按鈕在同一行就需要TableRow,但是超出的部分不會換行,不會顯示。

TableLayout布局常用屬性案例代碼
TableLayout布局常用屬性案例代碼

常用屬性

android:collapseColumns 設定需要被隐藏的列的序号,從0開始

android:stretchColumns 設定允許被拉伸的列的列序号,從0開始

android:shrinkColumns 設定允許被收縮的列的列序号,從0開始

子控件設定屬性

android:layout_column 顯示在第幾列(預設第0列)

android:layout_span 橫向跨幾列

隐藏

TableLayout布局常用屬性案例代碼

常用屬性中的拉伸,占用的是剩餘空間,沒有空間就不會被拉伸,這有4個按鈕(包括隐藏的那個),5個按鈕的話就不會拉伸

TableLayout布局常用屬性案例代碼
TableLayout布局常用屬性案例代碼

常用屬性中的收縮,需要有按鈕超出螢幕, 如果沒超出螢幕,不會收縮,由于之前按鈕長度正好在邊緣,收縮的話相當于隐藏了,是以我更改的按鈕的text,讓按鈕在邊界超出一半

收縮前

TableLayout布局常用屬性案例代碼

收縮後

TableLayout布局常用屬性案例代碼
TableLayout布局常用屬性案例代碼

案例代碼

activity_main.xml

<?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:shrinkColumns="1"
    android:layout_column="2"
    >

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

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

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

    </TableRow>

    <TableRow>
        <Button
            android:text="第1個按鈕"
            android:layout_column="1"
            android:layout_span="2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

        <Button
            android:text="第2個按鈕"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </TableRow>

</TableLayout>
           
TableLayout布局常用屬性案例代碼