本節(jié)引言:前面我們已經(jīng)學(xué)習(xí)了平時(shí)實(shí)際開發(fā)中用得較多的線性布局(LinearLayout)與相對(duì)布局(RelativeLayout), 其實(shí)學(xué)完這兩個(gè)基本就夠用了,筆者在實(shí)際開發(fā)中用得比較多的也是這兩個(gè),當(dāng)然作為一個(gè)好學(xué)的程序猿, 都是喜歡刨根問題的,所以雖說用得不多,但是還是有必要學(xué)習(xí)一下基本的用法的,說不定哪一天能用得上呢! 你說是吧,學(xué)多點(diǎn)東西沒什么的,又不吃虧!好了,扯淡就扯到這里,開始這一節(jié)的學(xué)習(xí)吧,這一節(jié)我們會(huì)學(xué)習(xí) Android中的第三個(gè)布局:TableLayout(表格布局)! 1.本節(jié)學(xué)習(xí)路線圖
2.TableLayout的介紹
3.如何確定行數(shù)與列數(shù)
4.三個(gè)常用屬性
屬性使用示例: ①collapseColumns(隱藏列)流程:在TableRow中定義5個(gè)按鈕后,接著在最外層的TableLayout中添加以下屬性: android:collapseColumns = "0,2",就是隱藏第一與第三列,代碼如下: <TableLayout
android:id="@+id/TableLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:collapseColumns="0,2" >
<TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="one" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="two" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="three" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="four" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="five" />
</TableRow>
</TableLayout>
運(yùn)行效果圖:
②stretchColumns(拉伸列)流程:在TableLayout中設(shè)置了四個(gè)按鈕,接著在最外層的TableLayout中添加以下屬性: android:stretchColumns = "1" 設(shè)置第二列為可拉伸列,讓該列填滿這一行所有的剩余空間,代碼如下: <TableLayout
android:id="@+id/TableLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="1" >
<TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="one" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="two" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="three" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="four" />
</TableRow>
</TableLayout>
運(yùn)行效果圖:
③shrinkColumns(收縮列)步驟:這里為了演示出效果,設(shè)置了5個(gè)按鈕和一個(gè)文本框,在最外層的TableLayout中添加以下屬性: android:shrinkColumns = "1" 設(shè)置第二個(gè)列為可收縮列,代碼如下: <TableLayout
android:id="@+id/TableLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:shrinkColumns="1" >
<TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="one" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="two" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="three" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="four" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="five" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="文本XX" />
</TableRow>
</TableLayout>
運(yùn)行截圖:
從圖中我們可以看到two這個(gè)按鈕被擠壓成條條狀,這個(gè)就是收縮,為了保證表格能適應(yīng) 父容器的寬度!至于另外兩個(gè)屬性就不講解了,用法和HTML相同!有興趣的可以研究下! 5.使用實(shí)例使用TableLayout來完成簡(jiǎn)單的登錄界面,運(yùn)行效果圖如下:
流程解析:
|
|
|