詳解Android TableLayout表格布局
表格布局的標(biāo)簽是TableLayout,TableLayout繼承了LinearLayout。所以它依然是一個(gè)線性布局。
前言:
1、TableLayout簡(jiǎn)介
2、TableLayout行列數(shù)的確定
3、TableLayout可設(shè)置的屬性詳解
4、一個(gè)包含4個(gè)TableLayout布局的實(shí)例及效果圖
<?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個(gè)TableLayout,用于描述表中的列屬性。第0列可伸展,第1列可收縮 ,第2列被隱藏--> <TextView android:text="數(shù)字鍵盤(pán)" android:layout_height="wrap_content" android:layout_width="wrap_content" android:textSize="20sp" android:background="#7f00ffff"/> <TableLayout android:id="@+id/table2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="1dip"> <TableRow> <Button android:text="0"/> <Button android:text="1"/> <Button android:text="2"/> <Button android:text="+"/> <Button android:text="="/> </TableRow> <TableRow> <Button android:text="3"/> <Button android:text="4"/> <Button android:text="5"/> <Button android:text="-"/> <Button android:text="*"/> </TableRow> <TableRow> <Button android:text="6"/> <Button android:text="7"/> <Button android:text="8"/> <Button android:text="9"/> <Button android:text="/"/> </TableRow> </TableLayout> </LinearLayout>
一、Tablelayout簡(jiǎn)介
Tablelayout類以行和列的形式對(duì)控件進(jìn)行管理,每一行為一個(gè)TableRow對(duì)象,或一個(gè)View控件。
當(dāng)為T(mén)ableRow對(duì)象時(shí),可在TableRow下添加子控件,默認(rèn)情況下,每個(gè)子控件占據(jù)一列。
當(dāng)為View時(shí),該View將獨(dú)占一行。
二、TableLayout行列數(shù)的確定
TableLayout的行數(shù)由開(kāi)發(fā)人員直接指定,即有多少個(gè)TableRow對(duì)象(或View控件),就有多少行。
TableLayout的列數(shù)等于含有最多子控件的TableRow的列數(shù)。如第一TableRow含2個(gè)子控件,第二個(gè)TableRow含3個(gè),第三個(gè)TableRow含4個(gè),那么該TableLayout的列數(shù)為4.
三、TableLayout可設(shè)置的屬性詳解
TableLayout可設(shè)置的屬性包括全局屬性及單元格屬性。
1、全局屬性也即列屬性,有以下3個(gè)參數(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="*" 隱藏所有行
說(shuō)明:列可以同時(shí)具備stretchColumns及shrinkColumns屬性,若此,那么當(dāng)該列的內(nèi)容N多時(shí),將“多行”顯示其內(nèi)容。(這里不是真正的多行,而是系統(tǒng)根據(jù)需要自動(dòng)調(diào)節(jié)該行的layout_height)
2、單元格屬性,有以下2個(gè)參數(shù):
android:layout_column 指定該單元格在第幾列顯示
android:layout_span 指定該單元格占據(jù)的列數(shù)(未指定時(shí),為1)
示例:
android:layout_column="1" 該控件顯示在第1列
android:layout_span="2" 該控件占據(jù)2列
說(shuō)明:一個(gè)控件也可以同時(shí)具備這兩個(gè)特性。
<?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個(gè)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="我向行方向伸展,我可以很長(zhǎng) "/> <TextView android:text="我向列方向收縮,我可以很深"/> </TableRow> </TableLayout> <!-- 第2個(gè)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="我被指定在第2列" android:layout_column="2"/> </TableRow> <TableRow> <TextView android:text="我跨1到2列,不信你看!" android:layout_column="1" android:layout_span="2" /> </TableRow> </TableLayout> <!-- 第3個(gè)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="三個(gè)字" ></Button> </TableRow> </TableLayout> <!-- 第4個(gè)TableLayout,使用可伸展特性,并指定每個(gè)控件寬度一致,如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="三個(gè)字" android:layout_width="1dip"></Button> </TableRow> </TableLayout> <TextView android:text="表5:應(yīng)用三,均勻布局" android:layout_height="wrap_content" android:layout_width="wrap_content" android:textSize="15sp" android:background="#7f00ffff"/> <TableLayout android:id="@+id/table5" android:layout_width="fill_parent" android:layout_height="wrap_content" android:stretchColumns="*" android:padding="6dip" > <TableRow> <Button android:text="一" android:layout_width="1dip"></Button> <Button android:text="兩字" android:layout_width="1dip"></Button> <Button android:text="三個(gè)字" android:layout_width="1dip"></Button> <Button android:text="四個(gè)個(gè)字" android:layout_width="1dip"></Button> <Button style="?android:attr/buttonStyleSmall" android:layout_width="1dip" android:layout_height="wrap_content" android:text="New Button" android:id="@+id/button" /> </TableRow> </TableLayout> </LinearLayout>
以上內(nèi)容是小逼給大家介紹的Android TableLayout表格布局,希望對(duì)大家有所幫助!
相關(guān)文章
Android啟動(dòng)畫(huà)面的實(shí)現(xiàn)方法
這篇文章主要介紹了Android啟動(dòng)畫(huà)面的實(shí)現(xiàn)方法,分析了布局文件及加載啟動(dòng)文件的實(shí)現(xiàn)方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-01-01
Android 實(shí)現(xiàn)滑動(dòng)的六種方式
這篇文章主要給大家分享的是Android 實(shí)現(xiàn)滑動(dòng)的六種方式,分別是layout、scrollBy、offsetLeftAndRight offsetTopAndButton、LayoutParams、Scroller、平移動(dòng)畫(huà),需要的朋友可以參考一下下面文章的具體內(nèi)容2021-11-11
Android開(kāi)發(fā)進(jìn)階自定義控件之滑動(dòng)開(kāi)關(guān)實(shí)現(xiàn)方法【附demo源碼下載】
這篇文章主要介紹了Android開(kāi)發(fā)進(jìn)階自定義控件之滑動(dòng)開(kāi)關(guān)實(shí)現(xiàn)方法,結(jié)合實(shí)例形式詳細(xì)分析了Android自定義開(kāi)關(guān)控件的原理、實(shí)現(xiàn)步驟與相關(guān)操作技巧,需要的朋友可以參考下2016-08-08
Android解析json數(shù)據(jù)示例代碼(三種方式)
本篇文章主要介紹了Android解析json數(shù)據(jù)示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-03-03
Android應(yīng)用中炫酷的橫向和環(huán)形進(jìn)度條的實(shí)例分享
這篇文章主要介紹了Android應(yīng)用中炫酷的橫向和圓形進(jìn)度條的實(shí)例分享,文中利用了一些GitHub上的插件進(jìn)行改寫(xiě),也是一片很好的二次開(kāi)發(fā)教學(xué),需要的朋友可以參考下2016-04-04
Android ScrollView嵌套橫向滑動(dòng)控件時(shí)沖突問(wèn)題
本篇文章主要介紹了Android ScrollView嵌套橫向滑動(dòng)控件時(shí)沖突問(wèn)題,具有一定的參考價(jià)值,有興趣的可以了解一下2017-08-08
flutter 實(shí)現(xiàn)多布局列表的示例代碼
這篇文章主要介紹了flutter 實(shí)現(xiàn)多布局列表的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
Android編程判斷當(dāng)前指定App是否在前臺(tái)的方法
這篇文章主要介紹了Android編程判斷當(dāng)前指定App是否在前臺(tái)的方法,涉及Android針對(duì)進(jìn)程操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11

