Android:控件布局(表格布局)TableLayout


TableLayout繼承LinearLayout

實例:用表格布局實現計算機布局》》》》》》》》》》》》

 

有多少個TableRow對象就有多少行,

列數等於最多子控件的TableRow的列數

直接在TableLayout加控件,控件會占據一行

TableLayout屬性(也叫全局屬性):*代表所有列

android:shrinkColumns -------設置可收縮的列,(內容過多,則收縮,擴展到第二行,控件沒布滿TableLayout時不起作用)

android:stretchColumns ------設置可伸展的列,(有空白則填充)

列可以同時具備stretchColumns及shrinkColumns屬性

android:collapseColumns ------設置要隱藏的列(索引列從0開始)

 

內部控件屬性:

android:layout_column -------該單元格在第幾列顯示

android:layout_span    -------該單元格占據列數,默認為1

伸展收縮實例:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <!-- 表格1-伸展 -->

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ff0000"
        android:shrinkColumns="0,1,2" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="我占據一行" />

        <TableRow>

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="000000000000000000000000" >
            </Button>

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="111111111111111111111111" >
            </Button>

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

        <TableRow>

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="000000000000000000000000" >
            </Button>

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_span="2"
                android:text="我占據2列" >
            </Button>
        </TableRow>
    </TableLayout>

    <!-- 表格2-收縮 -->

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FFCD6B"
        android:stretchColumns="0,1" >

        <TableRow>

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="填充一" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="填充二" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="普通三" />
        </TableRow>
    </TableLayout>

</LinearLayout>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM