Android 布局之TableLayout

來源:互聯網
上載者:User

標籤:app   表格   xmlns   files   style   vertica   參數   button   utf-8   

1 TableLayout簡介

TableLayout是表格版面配置。TableLayout 可設定的屬性包括全域屬性及儲存格屬性。

1.1 全域屬性

有以下3個參數:

android:stretchColumns
設定可伸展的列。該列可以向行方向伸展,最多可佔據一整行。
android:shrinkColumns
設定可收縮的列。當該列子控制項的內容太多,已經擠滿所在行,那麼該子控制項的內容將往列方向顯示。
android:collapseColumns
設定要隱藏的列。

 

樣本:

android:stretchColumns="0"     ----  第0列可伸展
android:shrinkColumns="1,2"   ----  第1,2列皆可收縮
android:collapseColumns="*"    ----  隱藏所有行
說明:列可以同時具備stretchColumns及shrinkColumns屬性,若此,那麼當該列的內容N多時,將“多行”顯示其內容。(這裡不是真正的多行,而是系統根據需要自動調節該行的layout_height)

 

1.2 儲存格屬性

有以下2個參數:

android:layout_column
指定該儲存格在第幾列顯示
android:layout_span
指定該儲存格佔據的列數(未指定時,為1)

 

樣本:

android:layout_column="1"   ----  該控制項顯示在第1列
android:layout_span="2"       ----  該控制項佔據2列
說明:一個控制項也可以同時具備這兩個特性。

 

 

2 TableLayout樣本

建立一個activity,包含幾組TableLayout;每個TableLayout分別設定不同的屬性。

layout檔案

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:padding="3dip"    >        <!-- 第1個TableLayout,用於描述表中的列屬性。第0列可伸展,第1列可收縮 ,第2列被隱藏-->    <TextView         android:text="表1:全域設定:列屬性設定"        android:layout_height="wrap_content"         android:layout_width="wrap_content"        android:textSize="15sp"        android:background="#7f00ffff"/>    <TableLayout                        android:id="@+id/table1"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:stretchColumns="0"        android:shrinkColumns="1"        android:collapseColumns="2"        android:padding="3dip">        <TableRow>            <Button android:text="該列可伸展"/>            <Button android:text="該列可收縮"/>            <Button android:text="我被隱藏了"/>        </TableRow>                <TableRow>            <TextView android:text="我向行方向伸展,我可以很長"/>            <TextView android:text="我向列方向收縮,我可以很深; 我向列方向收縮,我可以很深"/>        </TableRow>                    </TableLayout>        <!-- 第2個TableLayout,用於描述表中儲存格的屬性,包括:android:layout_column 及android:layout_span-->    <TextView         android:text="表2:儲存格設定:指定儲存格屬性設定"        android:layout_height="wrap_content"         android:layout_width="wrap_content"        android:textSize="15sp"        android:background="#7f00ffff"/>        <TableLayout        android:id="@+id/table2"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:padding="3dip">        <TableRow>            <Button android:text="第0列"/>            <Button android:text="第1列"/>            <Button android:text="第2列"/>        </TableRow>                <TableRow>            <TextView android:text="我被指定在第1列" android:layout_column="1"/>        </TableRow>                    <TableRow>            <TextView                android:text="我跨1到2列,不信你看!"                android:layout_column="1"                android:layout_span="2"                />        </TableRow>            </TableLayout>            <!-- 第3個TableLayout,使用可伸展特性布局-->    <TextView         android:text="表3:應用一,非均勻布局"        android:layout_height="wrap_content"         android:layout_width="wrap_content"        android:textSize="15sp"        android:background="#7f00ffff"/>    <TableLayout        android:id="@+id/table3"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:stretchColumns="*"        android:padding="3dip"        >        <TableRow>            <Button android:text="一" ></Button>            <Button android:text="兩字"></Button>            <Button android:text="三個字" ></Button>        </TableRow>    </TableLayout>        <!-- 第4個TableLayout,使用可伸展特性,並指定每個控制項寬度一致,如1dip-->    <TextView         android:text="表4:應用二,均勻布局"        android:layout_height="wrap_content"         android:layout_width="wrap_content"        android:textSize="15sp"        android:background="#7f00ffff"/>    <TableLayout        android:id="@+id/table4"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:stretchColumns="*"        android:padding="3dip"        >        <TableRow>            <!--              -->            <Button android:text="一" android:layout_width="1dip"></Button>            <Button android:text="兩字" android:layout_width="1dip"></Button>            <Button android:text="三個字" android:layout_width="1dip"></Button>        </TableRow>    </TableLayout></LinearLayout>

 

點擊下載:原始碼

運行效果:

Android 布局之TableLayout

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.