日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android表格布局(Table Layout)

發(fā)布時(shí)間:2025/4/16 Android 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android表格布局(Table Layout) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Android表格布局(Table Layout)

?

先來看布局管理器之間繼承關(guān)系圖:


圖1

可知TableLayout繼承了LinearLayout,所以表格布局本質(zhì)上依然是線性管理器。

?

表格布局采用行、列的形式來管理組件,它并不需要明確地聲明包含了多少行、多少列,而是通過添加TableRow、其他組件來控制表格的行數(shù)和列數(shù)。

?

每向TableLayout添加一個(gè)TableRow,該TableRow就是一個(gè)表格行,TableRow也是容器,因此它也可以不斷地添加組件,每添加一個(gè)子組件該表格就添加一列。

?

TableLayout一般以下面兩種方式實(shí)現(xiàn):

(1)??自己作為最頂層父容器

<!--定義一個(gè)TableLayout,有兩行第1列所有單元格的寬度可以被收縮,以保證該表格能適應(yīng)父容器的寬度第2列所有單元格的寬度可以拉伸,以保證能完全填滿表格空余空間--> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/TableLayout1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:shrinkColumns="1"android:stretchColumns="2"><!--這是此TableLayout的第1行,沒有使用TableRow,直接添加一個(gè)Button,那么次Button自己占用整行 --><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="獨(dú)自一行的按鈕1"/><!-- 這是第2行,先添加一個(gè)TableRow,并為TableRow添加三個(gè)Button,也就是此行包含三列 --><TableRow><Buttonandroid:id="@+id/button2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="普通按鈕1"/><Buttonandroid:id="@+id/button3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="被收縮的按鈕1"/><Buttonandroid:id="@+id/button4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="被拉伸的按鈕1"/></TableRow><!--這是此TableLayout的第3行,沒有使用TableRow,直接添加一個(gè)Button,那么次Button自己占用整行 --><Buttonandroid:id="@+id/button5"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="獨(dú)自一行的按鈕2"/><!-- 這是第4行,先添加一個(gè)TableRow,并為TableRow添加三個(gè)Button,也就是此行包含三列 --><TableRow><Buttonandroid:id="@+id/button6"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="普通按鈕2"/><Buttonandroid:id="@+id/button7"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="被收縮的按鈕2"/><Buttonandroid:id="@+id/button8"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="被拉伸的按鈕2"/></TableRow></TableLayout>

效果如下:


圖2

這里只有一個(gè)TableLayout,如果我們想單獨(dú)控制地4行,比如想把“普通按鈕2”隱藏,也就是增加android:collapseColumns="0",這樣會(huì)把“普通按鈕1”,這一列也隱藏了,如下圖:


圖3

但如果要實(shí)現(xiàn)只“普通按鈕2”這列,我們來看下面的實(shí)現(xiàn)

?

(2)??LinearLayout作為TableLayout的容器

?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent">--><!--定義第1個(gè)TableLayout,有兩行第1列所有單元格的寬度可以被收縮,以保證該表格能適應(yīng)父容器的寬度第2列所有單元格的寬度可以拉伸,以保證能完全填滿表格空余空間--> <TableLayoutandroid:id="@+id/TableLayout1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:shrinkColumns="1"android:stretchColumns="2"><!--這是此TableLayout的第1行,沒有使用TableRow,直接添加一個(gè)Button,那么次Button自己占用整行 --><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="獨(dú)自一行的按鈕1"/><!-- 這是第2行,先添加一個(gè)TableRow,并為TableRow添加三個(gè)Button,也就是此行包含三列 --><TableRow><Buttonandroid:id="@+id/button2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="普通按鈕1"/><Buttonandroid:id="@+id/button3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="被收縮的按鈕1"/><Buttonandroid:id="@+id/button4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="被拉伸的按鈕1"/></TableRow></TableLayout> <!--定義第2個(gè)TableLayout,有兩行第1列所有單元格的寬度可以被收縮,以保證該表格能適應(yīng)父容器的寬度第2列所有單元格的寬度可以拉伸,以保證能完全填滿表格空余空間--> <TableLayoutandroid:id="@+id/TableLayout2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:collapseColumns="0"android:shrinkColumns="1"android:stretchColumns="2"><!--這是此TableLayout的第3行,沒有使用TableRow,直接添加一個(gè)Button,那么次Button自己占用整行 --><Buttonandroid:id="@+id/button5"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="獨(dú)自一行的按鈕2"/><!-- 這是第4行,先添加一個(gè)TableRow,并為TableRow添加三個(gè)Button,也就是此行包含三列 --><TableRow><Buttonandroid:id="@+id/button6"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="普通按鈕2"/><Buttonandroid:id="@+id/button7"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="被收縮的按鈕2"/><Buttonandroid:id="@+id/button8"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="被拉伸的按鈕2"/></TableRow></TableLayout></LinearLayout>

效果如下:


圖4

?

通過在第2個(gè)TableLayout中增加android:collapseColumns="0"實(shí)現(xiàn),這里需要主要的是LinearLayout的android:orientation屬性值的設(shè)置,如果沒有這一項(xiàng)或是其值為horizontal,那么后面兩行都看不到,因?yàn)槭且运椒较蚺帕械?#xff0c;后面兩行顯示在前兩行的右邊,看不到。

總結(jié)

以上是生活随笔為你收集整理的Android表格布局(Table Layout)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。