TableLayout(表格佈局)
本節引言:
前面我們已經學習了平時實際開發中用得較多的線性佈局(LinearLayout)與相對佈局(RelativeLayout), 其實學完這兩個基本就夠用了,筆者在實際開發中用得比較多的也是這兩個,當然作為一個好學的程序猿, 都是喜歡刨根問題的,所以雖說用得不多,但是還是有必要學習一下基本的用法的,說不定哪一天能用得上呢! 你說是吧,學多點東西沒什麼的,又不吃虧!好了,扯淡就扯到這裡,開始這一節的學習吧,這一節我們會學習 Android中的第三個佈局:TableLayout(表格佈局)!
本節學習路線圖
#路線圖分析:從上面的路線圖,可以看出TableLayout的用法還是很簡單的,無非就是確定表格的行數,以及使用 那三個屬性來設定每一行中的第某列的元素隱藏,拉伸,或收縮即可!
2.TableLayout的介紹
相信學過HTML的朋友都知道,我們可以透過< table >< tr >< td >就可以產生一個HTML的表格, 而Android中也允許我們使用表格的方式來排列元件,就是行與列的方式,就說我們這節的TableLayout! 但卻不像我們後面會講到的Android 4.0後引入的GridLayout(網格)佈局一樣,直接就可以設定多少行與多少列!
3.如何決定行數與列數
#①如果我們直接在TableLayout中加入元件的話,那麼這個元件將會佔滿一行! ! !
②如果我們想一行上有多個元件的話,就要添加一個TableRow的容器,把元件都丟到裡面!
③tablerow中的元件個數就決定了該行有多少列,而列的寬度由該列中最寬的單元格決定
④tablerow的layout_width屬性,預設是fill_parent的,我們自己設定成其他的值也不會生效! ! ! 但是layout_height預設是wrapten——content的,我們卻可以自己設定大小!
⑤整個表格佈局的寬度取決於父容器的寬度(佔滿父容器本身)
⑥有多少行就要自己數啦,一個tablerow一行,一個單獨的組件也一行!多少列則是看tableRow中 的元件個數,元件最多的就是TableLayout的列數
#4.三個常用屬性
android:collapseColumns: 設定需要被隱藏的列的序號
android:shrinkColumns:設定允許被收縮的列的列序號
#android:stretchColumns:設定運行被拉伸的列的列序號以上這三個屬性的列號都是從0開始算的,比如shrinkColunmns = "2",對應的是第三列!
可以設定多個,用逗號隔開例如"0,2",如果是所有欄位都生效,則用" *"號碼即可
除了這三個常用屬性,還有兩個屬性,分別就是跳格子以及合併單元格,這和HTML中的Table類似:android:layout_column="2":表示的就是跳過第二個,直接顯示到第三個格子處,從1開始算的!
android:layout_span ="4":表示合併4個單元格,也就說這個元件佔4個單元格
屬性使用範例:
# ①collapseColumns(隱藏列)
流程:在TableRow中定義5個按鈕後,接著在最外層的TableLayout中加入下列屬性: android:collapseColumns = "0,2",就是隱藏第一與第三列,程式碼如下:
android:id="@+id/TableLayout2"
android:layout_width="fill_parent apseColumns= “0,2”>
<按鈕
# android:layout_width="wrap_content" > android:text="one" />
<按鈕
# android:layout_width="wrap_content" > android:text="two" />
<按鈕
# android:layout_width="wrap_content" > android:text="two" />
<按鈕
# android:layout_width="wrap_content" > android:text="four" />
<按鈕
# android:layout_width="wrap_content" > android:text=" Five" />
</表格佈局>
運行效果圖:
②stretchColumns(拉伸列)
流程:在TableLayout中設定了四個按鈕,接著在最外層的TableLayout中加入以下屬性: android:stretchColumns = "1"
設定第二列為可拉伸列,讓該列填滿這一行所有的剩餘空間,程式碼如下:
## android:id="@+id/TableLayout2"
and?
android:stretchColumns = “1”>
# <按鈕
out_height="wrap_content"
android:text="one" />
# <按鈕
out_height="wrap_content"
android:text="two" />
# <按鈕
out_height="wrap_content"
android:text="三" />
# <按鈕
out_height="wrap_content"
android:text="four" />
</表格佈局>
運行效果圖:
③shrinkColumns(收縮列)
#步驟:這裡為了示範出效果,設定了5個按鈕和一個文字框,在最外層的TableLayout中加入以下屬性: android:shrinkColumns = "1"
設定第二個列為可縮列,程式碼如下:
android:id="@+id/TableLayout2"
android:layout_width="fill_parent rinkColumns= “1”>
<按鈕
# android:layout_width="wrap_content" > android:text="one" />
<按鈕
# android:layout_width="wrap_content" > android:text="two" />
<按鈕
# android:layout_width="wrap_content" > android:text="two" />
<按鈕
# android:layout_width="wrap_content" > android:text="four" />
<按鈕
# android:layout_width="wrap_content" > android:text=" Five" />
#######################################################1 android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="文本XX" />
</TableRow>
</表格佈局>
運行截圖:
從圖中我們可以看到two這個按鈕被擠壓成條條狀,這個就是收縮,為了保證表格能適應 父容器的寬度!至於另外兩個屬性就不講解了,用法和HTML相同!有興趣的可以研究下!
5.使用實例
使用TableLayout來完成簡單的登入介面,運行效果圖如下:
流程解析:
①呼叫gravity屬性,設定為center_vertical,讓佈局裡面的元件在垂直方向上居中
②將TableLayout中的第一和第四列設定為可拉伸
③在每個TableRow中新增兩個TextView,用於拉伸填滿該行,這樣可以讓表格水平居中
android:stretchColumns= "0,3" 設定為0.3,是為了讓兩邊都充滿,那麼中間部分就可以居中了
詳細程式碼如下:
#<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/TableLayout1"
android:layout_width="match_parent" ## .MainActivity"
android: stretchColumns="0,3"
android:gravity="center_vertical"
android:background="../style/
;
android:layout_width="w_content "
android:text="使用者名稱:"/>
android:layout_width="5content content"
android:minWidth="150dp"/>
;
;
# android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密 码:"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="150dp"
/>
<TextView />
</TableRow>
<TableRow>
<TextView />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登陆"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="退出"/>
<TextView />
</TableRow>
</表格佈局>6.發現的問題
相信大家在使用這個TableLayout的TableRow的時候會遇到這個警告:
#當然,程式還是可以運作的,不過或許你是強迫症患者,看到黃色感嘆號你就不爽的話! 而解決這個警告的方法也是很奇葩的:只要你的TableLayout裡面有2個或以上的TableRow就可以了!
本節小結:
好的,關於Android的第三個佈局:TableLayout就到這裡~無非就是五個屬性的使用而已,實際開發 表格佈局我們用的不多,知道簡單的用法就可以了!