android:TableLayout表格佈局詳解

轉載自:http://blog.csdn.net/justoneroad/article/details/6835915

這篇博文包括的內容:

1、TableLayout簡介

2、TableLayout行列數的確定

3、TableLayout可設置的屬性詳解

4、一個包含4個TableLayout佈局的實例及效果圖


一、Tablelayout簡介

       Tablelayout類以行和列的形式對控件進行管理,每一行爲一個TableRow對象,或一個View控件。

         當爲TableRow對象時,可在TableRow下添加子控件,默認情況下,每個子控件佔據一列。

         當爲View時,該View將獨佔一行。

二、TableLayout行列數的確定

           TableLayout的行數由開發人員直接指定,即有多少個TableRow對象(或View控件),就有多少行。

        TableLayout的列數等於含有最多子控件的TableRow的列數。如第一TableRow含2個子控件,第二個TableRow含3個,第三個TableRow含4個,那麼該TableLayout的列數爲4.

三、TableLayout可設置的屬性詳解

TableLayout可設置的屬性包括全局屬性及單元格屬性。

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)

2、單元格屬性,有以下2個參數:

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

android:layout_span        指定該單元格佔據的列數(未指定時,爲1)

示例:

android:layout_column="1"    該控件顯示在第1列

android:layout_span="2"        該控件佔據2列

說明:一個控件也可以同時具備這兩個特性。


四、一個包含4個TableLayout佈局的實例及效果圖

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:padding="3dip"  
  7.     >  
  8.       
  9.     <!-- 第1個TableLayout,用於描述表中的列屬性。第0列可伸展,第1列可收縮 ,第2列被隱藏-->  
  10.     <TextView   
  11.         android:text="表1:全局設置:列屬性設置"  
  12.         android:layout_height="wrap_content"   
  13.         android:layout_width="wrap_content"  
  14.         android:textSize="15sp"  
  15.         android:background="#7f00ffff"/>  
  16.     <TableLayout               
  17.         android:id="@+id/table1"  
  18.         android:layout_width="fill_parent"  
  19.         android:layout_height="wrap_content"  
  20.         android:stretchColumns="0"  
  21.         android:shrinkColumns="1"  
  22.         android:collapseColumns="2"  
  23.         android:padding="3dip">  
  24.         <TableRow>  
  25.             <Button android:text="該列可伸展"/>  
  26.             <Button android:text="該列可收縮"/>  
  27.             <Button android:text="我被隱藏了"/>  
  28.         </TableRow>  
  29.           
  30.         <TableRow>  
  31.             <TextView android:text="我向行方向伸展,我可以很長    "/>  
  32.             <TextView android:text="我向列方向收縮,我可以很深"/>  
  33.         </TableRow>         
  34.           
  35.     </TableLayout>  
  36.       
  37.     <!-- 第2個TableLayout,用於描述表中單元格的屬性,包括:android:layout_column 及android:layout_span-->  
  38.     <TextView   
  39.         android:text="表2:單元格設置:指定單元格屬性設置"  
  40.         android:layout_height="wrap_content"   
  41.         android:layout_width="wrap_content"  
  42.         android:textSize="15sp"  
  43.         android:background="#7f00ffff"/>   
  44.     <TableLayout  
  45.         android:id="@+id/table2"  
  46.         android:layout_width="fill_parent"  
  47.         android:layout_height="wrap_content"  
  48.         android:padding="3dip">  
  49.         <TableRow>  
  50.             <Button android:text="第0列"/>  
  51.             <Button android:text="第1列"/>  
  52.             <Button android:text="第2列"/>  
  53.         </TableRow>  
  54.           
  55.         <TableRow>  
  56.             <TextView android:text="我被指定在第1列" android:layout_column="1"/>  
  57.         </TableRow>  
  58.               
  59.         <TableRow>  
  60.             <TextView  
  61.                 android:text="我跨1到2列,不信你看!"  
  62.                 android:layout_column="1"  
  63.                 android:layout_span="2"  
  64.                 />  
  65.         </TableRow>  
  66.           
  67.     </TableLayout>      
  68.       
  69.     <!-- 第3個TableLayout,使用可伸展特性佈局-->  
  70.     <TextView   
  71.         android:text="表3:應用一,非均勻佈局"  
  72.         android:layout_height="wrap_content"   
  73.         android:layout_width="wrap_content"  
  74.         android:textSize="15sp"  
  75.         android:background="#7f00ffff"/>  
  76.     <TableLayout  
  77.         android:id="@+id/table3"  
  78.         android:layout_width="fill_parent"  
  79.         android:layout_height="wrap_content"  
  80.         android:stretchColumns="*"  
  81.         android:padding="3dip"  
  82.         >  
  83.         <TableRow>  
  84.             <Button android:text="一" ></Button>  
  85.             <Button android:text="兩字"></Button>  
  86.             <Button android:text="三個字" ></Button>  
  87.         </TableRow>  
  88.     </TableLayout>  
  89.       
  90.     <!-- 第4個TableLayout,使用可伸展特性,並指定每個控件寬度一致,如1dip-->  
  91.     <TextView   
  92.         android:text="表4:應用二,均勻佈局"  
  93.         android:layout_height="wrap_content"   
  94.         android:layout_width="wrap_content"  
  95.         android:textSize="15sp"  
  96.         android:background="#7f00ffff"/>  
  97.     <TableLayout  
  98.         android:id="@+id/table4"  
  99.         android:layout_width="fill_parent"  
  100.         android:layout_height="wrap_content"  
  101.         android:stretchColumns="*"  
  102.         android:padding="3dip"  
  103.         >  
  104.         <TableRow>  
  105.             <Button android:text="一" android:layout_width="1dip"></Button>  
  106.             <Button android:text="兩字" android:layout_width="1dip"></Button>  
  107.             <Button android:text="三個字" android:layout_width="1dip"></Button>  
  108.         </TableRow>  
  109.     </TableLayout>  
  110. </LinearLayout>  

說明:第4個TableLayout裏的均勻佈局的均勻效果是有限的。其有限性體現在,當該行有N列,則每列的控件內容不能多於1/N。

運行效果圖:(如圖1)


圖1 TableLayout運行結果圖


參考書目:

[1] 《android基礎教程》,[美]Ed Burnette 著,張波,高朝勤,楊月等譯,北京:人民郵電出版社,2009.11

[2] 《android開發入門教程》,[美]Mark L. Murphy著,李雪飛,吳明暉譯,北京:人民郵電出版社,2010.12

[3] 《android核心技術與實例詳解》,吳亞峯,索依娜,北京:電子工業出版社,2010.10


參考文章:

Android 使用 TableLayout 佈局拉伸寬度

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章