本文轉自http://tech.it168.com/a2011/1213/1287/000001287977.shtml
在最新的Android 4.0 SDK中,新引入了GridLayout的布局樣式,這個布局樣式看上去可能有點象之前的TableLayout,但實際上還是有所不同的。
在上一篇教程中(http://tech.it168.com/a2011/1122/1277/000001277274.shtml),我們初步學習了解了GridLayout的布局基本知識,通過學習知道,GridLayout可以用來做一個象TableLayout這樣的布局樣式,但其性能及功能都要比tablelayout要好,比如GridLayout的布局中的單元格可以跨越多行,而tablelayout則不行,此外,其渲染速度也比tablelayout要快。在本文中,将指導讀者進一步加深對GridLayout的認識,帶大家實做一個簡單的數字鍵盤布局,從中體會GridLayout的用法。
開始設計
首先,我們先設計下将要設計的鍵盤布局圖,如下圖:

可以看到這個布局的一些特點:
1) 有5行4列
2)每行的單元格和列方向的單元格的大小都是不一定相等的,比如“+”号這個按鈕,在縱向上是橫跨了兩行的
可以看到,如果要用傳統的tablelayout布局樣式,要實作以上的布局,可能要外加嵌套linarlayout布局樣式,這樣就會使的布局設計十分麻煩,而如果有了GridLayout樣式,則可以很容易地解決這些問題。
GridLayout布局政策
GridLayout布局樣式和LinearLayout樣式一樣,可以有水準和垂直兩個方向的布局方式。即如果設定為垂直方向布局,則下一個單元格将會在下一行的同一位置或靠右一點的位置出現,而水準方向的布局,則意味着下一個單元格将會在目前單元格的右邊出現,也有可能會跨越下一行(因為有可能GridLayout布局樣式中。在我們的這個例子中,如果從最右邊的除号算起,使用水準布局的話則是4列,其代碼如下所示:
1 <GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
2 android:layout_width="wrap_content"
3 android:layout_height="wrap_content"
4 android:layout_gravity="center"
5 android:columnCount="4"
6 android:orientation="horizontal" >
7 </GridLayout>
定義簡單的單元格
在GridLayout中,定義每個子控件跟以前使用布局中定義的方法有點不同,預設的是對所有的子控件使用wrap_content的方式,而不是顯式聲明寬度和高度并使用wrap_conent和match_parent,更多的相關規則可以參考GridLayout的文檔,這裡隻需要在GridLayout本身的屬性中,定義android:layout_width 均為wrap_conent即可。
是以,我們接着在控件中,添加各個數字按鈕,如下:
<Button android:text="1" />
<Button android:text="2" />
………………………
運作後,可以看到一個初步的效果如下圖所示:
定義特殊符号的位置
可以看到,跟草稿的圖相比,象除号,等于号等,位置不是很吻合,下面我們作些相應的調整,如下:
1) 除号的大小可以不變,但它應該被放置在第4列出現
2) +号應該放在數字9之後,并且它的高度要占3行之多
3) 數字0應該占據兩列的寬度
4) 等于号應該占據三列
為此,修改代碼如下:
1 <?xml version="1.0" encoding="utf-8"?>
2 <GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="wrap_content"
4 android:layout_height="wrap_content"
5 android:layout_gravity="center"
6 android:columnCount="4"
7 android:orientation="horizontal" >
8
9 <Button
10 android:layout_column="3"
11 android:text="/" />
12
13 <Button android:text="1" />
14
15
16 <Button android:text="9" />
17
18 <Button
19 android:layout_rowSpan="3"
20 android:text="+" />
21
22 <Button
23 android:layout_columnSpan="2"
24 android:text="0" />
25
26 <Button android:text="00" />
27
28 <Button
29 android:layout_columnSpan="3"
30 android:text="=" />
31
32 </GridLayout>
在上面的代碼中,可以看到,數字鍵3中,通過使用android:layout_column="3"指定數字從第4列開始(注意列的序号從0開始),而+号是緊跟在數字鍵9後,并且用android:layout_rowSpan="3"指出位于第4行,符号等于,則是緊跟着在數字“00”後,由于其layout_columnSpan=3,可以看到,占據了3個列的位置,是以另外重新新起一行進行了布局。運作後的結果如下圖所示:
美化頁面布局
我們可以看到在上圖中,依然出現了很多空位,跟我們預想的草稿圖有一定差距,這裡其實可以調整每個數字按鈕中的位置即可,可以利用android 4.0 GridLayout布局中的
layout_gravity屬性,設定每個按鈕中的位置,隻需要設定layout_gravity屬性為fill,即可将每個控件填充到其layout_columnSpan及layout_rowSpan所指定的寬度,修改後的代碼如下所示:
1 <?xml version="1.0" encoding="utf-8"?>
2 <GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="wrap_content"
4 android:layout_height="wrap_content"
5 android:layout_gravity="center"
6 android:columnCount="4"
7 android:orientation="horizontal" >
8
9 <Button
10 android:layout_column="3"
11 android:text="/" />
12
13 <Button android:text="1" />
14
15 <Button android:text="2" />
16
17 <Button android:text="3" />
18
19 <Button android:text="*" />
20
21 <Button android:text="4" />
22
23 <Button android:text="5" />
24
25 <Button android:text="6" />
26
27 <Button android:text="-" />
28
29 <Button android:text="7" />
30
31 <Button android:text="8" />
32
33 <Button android:text="9" />
34
35 <Button
36 android:layout_gravity="fill"
37 android:layout_rowSpan="3"
38 android:text="+" />
39
40 <Button
41 android:layout_columnSpan="2"
42 android:layout_gravity="fill"
43 android:text="0" />
44
45 <Button android:text="00" />
46
47 <Button
48 android:layout_columnSpan="3"
49 android:layout_gravity="fill"
50 android:text="=" />
51
52 </GridLayout>
運作後,結果如下圖: