Android表格布局(Table Layout)
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)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 物联网相关网站
- 下一篇: android Timer与TimerT