Android五種佈局管理器之『TableLayout』

表格佈局(TableLayout)類以行和列的形式管理控件,每一行爲一個TableRow對象,也可以作爲一個View對象;當爲View對象時,該View對象將跨越該行的所有列。在TableRow中可以添加子控件,每添加一個子控件即爲一列。

TableLayout佈局中並不會爲每一行、每一列或每個單元格繪製邊框,每一行可以有0個或多個單元格,每個單元格爲一個View對象。TableLayout中可以有空的單元格,單元格也可以像HTML中那樣跨越多個列。

在TableLayout佈局中,一個列的寬度由該列中最寬的那個單元格指定,而表格的寬度是由父容器指定的。在TableLayout中,可以爲列設置三種屬性:

  • Shrinkable:如果一個列被標識爲Shrinkable,則該列的寬度可以進行收縮,以使表格能夠適應其父容器的大小。
  • Stretchable:如果一個列被標識爲Stretchable,則該列的寬度可以進行拉伸,以使填滿表格中的空閒空間。
  • Collapsed:如果一個列被標識爲Collapsed,則該列會被隱藏。

注意:一個列可以同時具有Shrinkable屬性和Stretchable屬性,在這種情況下,該列的寬度將任意拉伸或收縮以適應父容器。

TableLayout繼承自LinearLayout類,除了繼承來自父類的屬性和方法,TableLayout類中還包含表格佈局所特有的屬性和方法,如下表:

屬性名稱 對應方法 描述
android:collapseColumns setColumnCollapsed(int,boolean) 設置指定列號的列屬性爲Collapsed
android:shrinkColumns setShrinkAllColumns(boolean) 設置指定列號的列屬性爲Shrinkable
android:stretchColumns setStretchAllColumns(boolean) 設置指定列號的列屬性爲Stretchable

注意:TableLayout中所謂的列序號是從0開始計算的。setShrinkAllColumns和setStretchAllColumns實現的功能是將表格中的所有列設置爲Shrinkable或Stretchable。

下面來看一下效果:

點擊放大圖片

其中Main.xml代碼如下:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout  
  3.    android:id="@+id/LinearLayout01"  
  4.    android:layout_width="fill_parent"  
  5.    android:layout_height="fill_parent"  
  6.    xmlns:android="http://schemas.android.com/apk/res/android" 
  7.    android:orientation="vertical" 
  8.    android:background="@drawable/android" 
  9.    android:gravity="bottom"> 
  10.     
  11.    <!-- 第一行 --> 
  12.    <TableLayout  
  13.    android:id="@+id/TableLayout01"  
  14.    android:layout_width="fill_parent"  
  15.    android:layout_height="wrap_content"    
  16.    android:background="#FFFFFF" 
  17.    xmlns:android="http://schemas.android.com/apk/res/android"> 
  18.        <TextView  
  19.          android:text="我是單獨的一行"  
  20.          android:id="@+id/TextView01"  
  21.          android:layout_width="wrap_content"  
  22.          android:layout_height="wrap_content" 
  23.          android:layout_centerInParent="true" 
  24.          android:background="#fd8d8d" 
  25.          android:textColor="#000000" 
  26.          android:layout_margin="4px" 
  27.        > 
  28.        </TextView> 
  29.    </TableLayout> 
  30.     
  31.    <TableLayout  
  32.        android:id="@+id/TableLayout02"  
  33.        android:layout_width="fill_parent"  
  34.        android:layout_height="wrap_content"   
  35.        android:background="#FFFFFF" 
  36.        android:stretchColumns="0" 
  37.        xmlns:android="http://schemas.android.com/apk/res/android"> 
  38.        <!-- android:stretchColumns="0" 設置0號列爲可伸展的列,當有多個列可伸展時用逗號隔開 --> 
  39.        <!-- 第二行 --> 
  40.        <TableRow  
  41.          android:id="@+id/TableRow01"  
  42.          android:layout_width="wrap_content"  
  43.          android:layout_height="wrap_content"> 
  44.             <!-- 第一列 --> 
  45.             <TextView  
  46.              android:text="我是被拉上的一列"  
  47.              android:id="@+id/TextView02"  
  48.              android:layout_width="wrap_content"  
  49.              android:layout_height="wrap_content" 
  50.              android:layout_centerInParent="true" 
  51.              android:background="#9cfda3" 
  52.              android:textColor="#000000" 
  53.              android:layout_margin="4px">    
  54.             </TextView>  
  55.             <!-- 第二列 --> 
  56.             <TextView  
  57.              android:text="我的內容少"  
  58.              android:id="@+id/TextView03"  
  59.              android:layout_width="wrap_content"  
  60.              android:layout_height="wrap_content" 
  61.              android:layout_centerInParent="true" 
  62.              android:background="#8d9dfd" 
  63.              android:textColor="#000000" 
  64.              android:layout_margin="4px" 
  65.             >    
  66.             </TextView>          
  67.        </TableRow>  
  68.     </TableLayout> 
  69.      
  70.     <TableLayout  
  71.        android:id="@+id/TableLayout03"  
  72.        android:layout_width="fill_parent"  
  73.        android:layout_height="wrap_content"   
  74.        android:background="#FFFFFF" 
  75.        android:collapseColumns="1" 
  76.        android:shrinkColumns="0" 
  77.        xmlns:android="http://schemas.android.com/apk/res/android"> 
  78.        <!-- android:collapseColumns="1" 隱藏編號爲1的列,若有多個列要隱藏,則用逗號隔開,如0,2 --> 
  79.        <!-- android:shrinkColumns="0"  
  80.                     設置0號列爲可收縮的列,可收縮的列會縱向擴展 
  81.                     若有多個列要收縮,則用逗號隔開,如0,2 --> 
  82.                      
  83.        <!-- 第三行 --> 
  84.        <TableRow  
  85.          android:id="@+id/TableRow02"  
  86.          android:layout_width="wrap_content"  
  87.          android:layout_height="wrap_content"> 
  88.             <!-- 第一列 --> 
  89.             <TextView  
  90.              android:text="我的被收縮的一列被收縮的一列"  
  91.              android:id="@+id/TextView04"  
  92.              android:layout_width="wrap_content"  
  93.              android:layout_height="wrap_content" 
  94.              android:layout_centerInParent="true" 
  95.              android:background="#9cfda3" 
  96.              android:textColor="#000000" 
  97.              android:layout_margin="4px">    
  98.             </TextView>  
  99.             <!-- 第二列,被設置爲隱藏了 --> 
  100.             <TextView  
  101.              android:text="我的內容少"  
  102.              android:id="@+id/TextView05"  
  103.              android:layout_width="wrap_content"  
  104.              android:layout_height="wrap_content" 
  105.              android:layout_centerInParent="true" 
  106.              android:background="#8d9dfd" 
  107.              android:textColor="#000000" 
  108.              android:layout_margin="4px" 
  109.             >    
  110.             </TextView>  
  111.             <!-- 第三列 --> 
  112.             <TextView  
  113.              android:text="我的內容比較長比較長比較長"  
  114.              android:id="@+id/TextView06"  
  115.              android:layout_width="wrap_content"  
  116.              android:layout_height="wrap_content" 
  117.              android:layout_centerInParent="true" 
  118.              android:background="#fd8d8d" 
  119.              android:textColor="#000000" 
  120.              android:layout_margin="4px" 
  121.             >    
  122.             </TextView>          
  123.        </TableRow>    
  124.     </TableLayout>   
  125. </LinearLayout> 

Activity代碼爲:

  1. package com.sunchis; 
  2.  
  3. import android.app.Activity; 
  4. import android.os.Bundle; 
  5.  
  6. public class Android extends Activity {  
  7.     @Override 
  8.     public void onCreate(Bundle savedInstanceState) { 
  9.         super.onCreate(savedInstanceState); 
  10.         setContentView(R.layout.main);          //設置屏幕 
  11.     } 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章