這篇博文包括的內(nèi)容:
1袍辞、TableLayout簡介
2毅贮、****TableLayout行列數(shù)的確定
3、****TableLayout可設(shè)置的屬性詳解
4不从、一個包含4個TableLayout布局的實例及效果圖
一、Tablelayout簡介
Tablelayout類以行和列的形式對控件進(jìn)行管理牵敷,每一行為一個TableRow對象胡岔,或一個View控件。
當(dāng)為TableRow對象時枷餐,可在TableRow下添加子控件靶瘸,默認(rèn)情況下,每個子控件占據(jù)一列毛肋。
當(dāng)為View時怨咪,該View將獨占一行。
二润匙、TableLayout行列數(shù)的確定
TableLayout的行數(shù)由開發(fā)人員直接指定诗眨,即有多少個TableRow對象(或View控件),就有多少行孕讳。
TableLayout的列數(shù)等于含有最多子控件的TableRow的列數(shù)匠楚。如第一TableRow含2個子控件,第二個TableRow含3個厂财,第三個TableRow含4個芋簿,那么該TableLayout的列數(shù)為4.
三、TableLayout可設(shè)置的屬性詳解
TableLayout可設(shè)置的屬性包括全局屬性及單元格屬性璃饱。
1与斤、全局屬性也即列屬性,有以下3個參數(shù):
Android:stretchColumns 設(shè)置可伸展的列荚恶。該列可以向行方向伸展撩穿,最多可占據(jù)一整行。
android:shrinkColumns 設(shè)置可收縮的列裆甩。當(dāng)該列子控件的內(nèi)容太多冗锁,已經(jīng)擠滿所在行齐唆,那么該子控件的內(nèi)容將往列方向顯示嗤栓。
android:collapseColumns 設(shè)置要隱藏的列。
示例:
android:stretchColumns="0" 第0列可伸展
android:shrinkColumns="1,2" 第1,2列皆可收縮
android:collapseColumns="*" 隱藏所有行
說明:列可以同時具備stretchColumns及shrinkColumns屬性箍邮,若此茉帅,那么當(dāng)該列的內(nèi)容N多時,將“多行”顯示其內(nèi)容锭弊。(這里不是真正的多行堪澎,而是系統(tǒng)根據(jù)需要自動調(diào)節(jié)該行的layout_height)
2、單元格屬性味滞,有以下2個參數(shù):
android:layout_column 指定該單元格在第幾列顯示
android:layout_span 指定該單元格占據(jù)的列數(shù)(未指定時樱蛤,為1)
示例:
android:layout_column="1" 該控件顯示在第1列
android:layout_span="2" 該控件占據(jù)2列
說明:一個控件也可以同時具備這兩個特性钮呀。
四、一個包含4個TableLayout布局的實例及效果圖
<?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:全局設(shè)置:列屬性設(shè)置"
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:單元格設(shè)置:指定單元格屬性設(shè)置"
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:應(yīng)用一,非均勻布局"
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:應(yīng)用二惋啃,均勻布局"
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>
說明:第4個TableLayout里的均勻布局的均勻效果是有限的哼鬓。其有限性體現(xiàn)在,當(dāng)該行有N列边灭,則每列的控件內(nèi)容不能多于1/N异希。
運行效果圖:(如圖1)
圖1 TableLayout運行結(jié)果圖
參考書目:
[1] 《android基礎(chǔ)教程》,[美]Ed Burnette 著绒瘦,張波称簿,高朝勤,楊月等譯惰帽,北京:人民郵電出版社憨降,2009.11
[2] 《android開發(fā)入門教程》,[美]Mark L. Murphy著该酗,李雪飛授药,吳明暉譯,北京:人民郵電出版社呜魄,2010.12
[3] 《android核心技術(shù)與實例詳解》悔叽,吳亞峰,索依娜爵嗅,北京:電子工業(yè)出版社娇澎,2010.10