close
  • TableLayout

TableLayout–表格佈局,以Word表格來思考就很清楚可以知道此佈局方式的特性了,整個Layout是一格大表格,設計者可以依照需求喜好將元件放在表格中的各位置,甚至可以設定在該格內的一些屬性,而TableLayout裡面是以TableRow來區別每一列,但是要注意的是,如果在同一個TableLayout他會以最多欄位的那個列來統一讓所有列都有相同的欄數,除非在裡面透過『android:layout_span』方法來讓某一格可以跨越欄數,藉此修改使每一列不會被固定格數。

 

下表為一些屬性設定介紹(出處:iteye_android

屬性  說明
TableLayout属性
 android:collapseColumns 将TableLayout里面指定的列隱藏,隱藏多行需用","隔开,起始值為0
 android:stretchColumns 設置指定的列為可延伸的,填满剩下的多餘空白空間,填滿多行需用","隔开,起始值為0
 android:shrinkColumns 設置指定的列為可收缩的列,多行時需用","隔開,起始值為0
使用率較高的屬性
 android:layout_colum 設定該元件在TableRow中指定的列
 android:layout_span 設定該元件所跨越的列数

 

 

TableLayout:

本例中因為要讓TableLayout更好顯示在每一列所以在最外層用LinearLayout包起來,所以當我們在裡面寫了第二個TableLayout則會是在LinearLayout中的第二列去作顯示,透過這種方式可以很簡單得來排版我們常用的小算盤。

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingLeft="10dp" android:paddingRight="10dp" android:paddingTop="10dp">

    <TableLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:shrinkColumns="0"
        android:stretchColumns="1">

        <TableRow>
            <Button
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="But 1"/>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="But 2"/>
            <Button
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="But3"/>
        </TableRow>
        <TableRow>
            <Button
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="But 4:因為在同一個TableLayout所以But1所佔的空格會被跟著拉長"/>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="But 5"
                android:layout_span="2"/>
        </TableRow>
    </TableLayout>

    <TableLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:collapseColumns="1"
        android:stretchColumns="0">
        
        <TableRow>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="But 6:空白被我補滿了"/>
            <Button
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="But 7:這一列被隱藏了"/>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="But 8"/>
        </TableRow>
    </TableLayout>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="But 9"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="因為設定第二列隱藏的關係,所以But 7消失了!"
        android:textSize="25dp"
        />
</LinearLayout>

 TableLayout 

 

 

觀看其它種Layout:

 

參考資料:Corn-Android學習手札iteye_android屌絲技術之旅

arrow
arrow
    全站熱搜

    fiend1120 發表在 痞客邦 留言(2) 人氣()