Android TableLayout表格佈局

一:TableLayout是表格佈局先上例子:html

定義一個xml佈局文件tablelayout.xml:java

內容以下:android

<?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:gravity="center_horizontal">
  <TableLayout 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:stretchColumns="0,1,2"    
    android:shrinkColumns="1,2"
   >
   <TableRow>        
       <TextView            
           android:layout_column="1"            
           android:text="姓名"            
           android:gravity="center"
           android:background="#BBFFFF"/>        
       <TextView            
           android:text="基本信息"            
           android:gravity="center"
           android:background="#CD00CD"/>    
       <TextView            
           android:text="大大的人人兒呢"            
           android:gravity="center"
           android:background="#A020F0"/>    
    </TableRow>
  </TableLayout>
</LinearLayout>

 

package com.test;

import android.app.Activity;
import android.os.Bundle;

public class TableLayoutAndRowDemo extends Activity {
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tablelayout);
	}

}

效果:app

內容講解:ide

TableLayout,表格佈局採用行列形式管理UI組件,TableLayout不須要明確地聲明有多少行和列,而是經過添加TableRow、其它組件來控制表格的行數、列數。
v每次向TableLayout添加一個TableRow,就是在向表格添加一行,TableRow也是容器,能夠向TableRow中添加組件,每添加一個組件,便是添加一列。
v若是直接向TableLayout添加組件,則認爲這個組件佔用一行。
v表格佈局中列的寬度便是每一列中最寬的組件的寬度。 函數

 android:collapseColumns="1---隱藏該TableLayout裏的TableRow的列1,即第2列(從0開始計算),如有多列要隱藏,用「,」隔開。setColumnCollapsed(int,boolean)佈局

android:stretchColumns="0,1,2"----設置列0、一、2爲可伸展列。setColumnCollapsed(int,boolean)spa

android:shrinkColumns="1,2"-----設置列一、2爲可收縮列。當LayoutRow裏面的控件尚未佈滿佈局時,shrinkColumns不起做用 setColumnCollapsed(int,boolean)code

android:background="@drawable/picture_name"----本例中沒有涉及此屬性,它是要設置當前view 的背景圖片,圖片文件應該放在res文件夾下。xml

android:layout_column="1":表示控件放在標號爲1的列上,標號是從0開始的
android:layout_span表示一個控件佔幾列空間 
能夠用」*」來表示全部列,同一列能夠同時設置爲shrinkable和stretchable

 函數:

public TableLayout (Context context);
//爲給定的上下文建立表格佈局。
//參數   context  應用程序上下文

 

public TableLayout (Context context, AttributeSet attrs)
//使用指定的屬性集合爲給定的上下文建立表格佈局。
//參數    context  應用程序上下文     attrs        屬性集合

 

public void addView (View child)
public void addView (View child, int index)
public void addView (View child, int index, ViewGroup.LayoutParams params)
//添加子視圖。若是子視圖沒有設置佈局參數,則使用視圖組(ViewGroup)的佈局參數爲//該視圖佈局。index  子視圖加入的位置索引
public void addView (View child, ViewGroup.LayoutParams params)
//使用指定的佈局參數添加子視圖。
//參數    child         添加的子視圖   params  設置到子視圖上的佈局參數
public TableLayout.LayoutParams generateLayoutParams (AttributeSet attrs)
//返回一組基於提供的屬性集合的佈局參數集合。
//參數
//attrs        用於生成佈局參數的屬性集
//返回值        ViewGroup.LayoutParams或其子類的實例

 

public boolean isColumnCollapsed (int columnIndex)
//返回指定列的摺疊狀態。
//參數   columnIndex  列索引
//返回值     摺疊時爲true;不然爲false 

public boolean isColumnShrinkable (int columnIndex)
//返回指定的列是否可收縮。
//參數      columnIndex  列索引
//返回值    若是列能夠收縮,返回true;不然返回false

 

public boolean isColumnStretchable (int columnIndex)
//返回指定的列是否可拉伸。
//參數       columnIndex  列索引
//返回值     若是列能夠拉伸,返回true;不然返回false

 

public boolean isShrinkAllColumns ()
//指示是否全部的列都是可收縮的。
//返回值
// 若是全部列均可收縮,返回true;不然返回false

 

public boolean isStretchAllColumns ()
//指示是否全部的列都是可拉伸的。
//返回值    若是全部列均可拉伸,返回true;不然返回false

 

public void requestLayout ()
//當某些變動致使視圖的佈局失效時調用該方法。該方法按照視圖樹的順序調用。

 
public void setColumnCollapsed (int columnIndex, boolean isCollapsed)

  //摺疊或恢復給定列。摺疊時,列從屏幕上消失,其空間由其它列佔用。 當列屬於 TableRow 時才能夠進行摺疊/恢復操做。

  //調用該方法會請求佈局操做。
相關XML屬性    android:collapseColumns
參數    columnIndex  列索引
isCollapsed     摺疊時爲true;不然爲false

 

public void setColumnShrinkable (int columnIndex, boolean isShrinkable)
//設置指定列是否可收縮。當行太寬時,表格能夠收縮該列以提供更多空間。
//調用該方法會請求佈局操做。
//相關XML屬性    android:shrinkColumns
//參數    columnIndex  列索引
//         isShrinkable    若是列能夠收縮,設爲真;不然設爲假。默認是假。

 

public void setColumnStretchable (int columnIndex, boolean isStretchable)
//設置指定列是否可拉伸。可拉伸時,列會盡量多的佔用行中的可用空間。
//調用該方法會請求佈局操做。
//相關XML屬性    android:stretchColumns
//參數    columnIndex  列索引
//         isStretchable 若是列能夠拉伸,設爲真;不然設爲假.默認是假

 

public void setOnHierarchyChangeListener (ViewGroup.OnHierarchyChangeListener listener)
註冊當從視圖中添加或移除子視圖時發生的回調函數。
參數

  listener  層次結構變動時執行的回調函數
public void setShrinkAllColumns (boolean shrinkAllColumns)

  標記全部列爲可收縮的便利的方法。

  相關XML屬性   android:shrinkColumns
  參數   shrinkAllColumns    若是標記全部列爲可收縮時爲true

 public void setStretchAllColumns (boolean stretchAllColumns)

  標記全部列爲可拉伸的便利的方法。
相關XML屬性   android:stretchColumns
參數    stretchAllColumns           若是標記全部列爲可拉伸時爲true

 三:TableLayout實現邊框

  爲了醒目,須要給TableLayout設定邊框來區分不一樣的表格中的信息:

主要是經過設定TableLayout、TableRow 、View顏色反襯出邊框的顏色。

例如TableLayout的android:layout_margin="2dip"設置爲這個數字 ,在指定一個背景色android:background="#00ff00",它裏面的顏色也是這樣子的設置,就能夠呈現出帶邊框的效果了。

 (2)關於分割線:
咱們能夠經過在兩個TableRow之間添加一個<View>,並設置它的layout_height="1dip"和background來解決。但這樣就多產生了一個View,有點浪費。
其實還能夠設置TableRow的bottomMargin=1來產生一條分割線。 
  3.滾動:

關於TableLayout和HorizontalScrollView一塊兒使用時的寬度問題
我有一個TableLayout,它的內容是動態生成的。我遇到了下面的問題:
當動態生成的一行的內容太長時,靠右邊的內容會被遮住了。因而我想要這個TableLayout在橫向上能夠滾動。
解決的辦法是,用HorizontalScrollView包裝TableLayout,這樣,當內容很長時,就會出現橫向滾動條。
像這樣: 

<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView  
 android:layout_width="fill_parent"  
 android:layout_height="fill_parent">  
  <TableLayout  
  android:id="@+id/tl_forcast_result"  
  android:layout_width="fill_parent"  
  android:layout_height="wrap_content"  
  android:padding="10dp">    
  </TableLayout>  
 </HorizontalScrollView>

  但此時又出現了另外一個問題,加上HorizontalScrollView後,雖然我已經設了TableLayout的寬度是fill_parent。但當內容較少時,TableLayout仍是根據內容自適應寬度,不能滿屏。 此時,須要設置一個屬性就能解決問題了。設置HorizontalScrollView的android:fillViewport="true"。也就是設置是否將HorizontalScrollView的內容寬度拉伸以適應視口(viewport)

相關文章
相關標籤/搜索