標籤:android ui 布局 網格 gridlayout
說明:網格布局是4.0之後添加的布局,跟TableLayout有點像,但更加好用,它把容器分為一個rows*columns的網格,每個網格都是一個組件位,可是通過設定讓組件位佔據多行/列。
與之相似地,還有一個叫做GridView的組件,無論功能和名稱都很相似,不過GridView使用Adapter來填充組件位,GridLayout則要簡化得多。
按照CSDN博主studyboyjlu4(部落格地址:http://blog.csdn.net/studyboyjlu4)的看法,GridView和GridLayout的區別就和ListView和LinearLayout的區別類似。
GridLayout有兩類需要關注的屬性:
1 GridLayout本身的屬性
說明:這類屬性是針對GridLayout自身來設定的,主要是對行和列,以及對其方式的設定。
屬性工作表如下:
| 屬性 |
對應方法 |
屬性說明 |
| android:columnCount |
setColumCount(int) |
設定布局的最大列數 |
| android:rowCount |
setRowCount(int) |
設定布局的最大行數 |
| android:alignmentMode |
setAilgnmentMode(int) |
設定布局的對其方式(alignBounds: 對齊子視圖邊界;alignMargins: 對齊子視圖邊距;) |
| android:columnOrderPeserved |
setColumOrderPreserved(boolean) |
設定容器是否保留列序號 |
| android:rowOrderPeserved |
setRowOrderPeserved(boolean) |
設定容器是否保留行序號 |
| android:useDefaultMargins |
setUseDefaultMargins(boolean) |
設定容器是否使用預設的頁面邊界 |
2 針對容器內的子組件的屬性
說明:這類屬性是針對GridLayout中的子組件設定的,可以設定組件在網格中的大小和擺放方式。
屬性工作表如下:
| 屬性 |
對應方法 |
屬性說明 |
| android:layout_Gravity |
setGravity(int) |
設定組件如何佔據其所屬網格的空間 |
| android:layout_column |
|
設定組件在容器的第幾列 |
| android:layout_row |
|
設定組件在容器的第幾行 |
| android:layout_columnSpan |
|
設定組件佔據了幾列 |
| android:layout_rowSpan |
|
設定組件佔據了幾行 |
這裡的LayoutGravity跟一般的LayoutGravity有點區別,這裡的擺放位置是相對於所屬網格的,而不是對於布局父容器來說的。
<GridLayout android:columnCount="4" android:id="@+id/root1" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ImageView android:layout_rowSpan="2" android:layout_width="100dp" android:layout_height="200dp" android:background="#600096" /> <ImageView android:layout_columnSpan="2" android:layout_width="200dp" android:layout_height="100dp" android:background="#006096" /> <ImageView android:layout_rowSpan="2" android:layout_width="100dp" android:layout_height="50dp" android:layout_gravity="bottom" android:background="#009660" /> <ImageView android:layout_columnSpan="2" android:layout_rowSpan="2" android:layout_width="200dp" android:layout_height="300dp" android:background="#960060" /> <ImageView android:layout_width="100dp" android:layout_height="200dp" android:background="#AA6060" /> <ImageView android:layout_width="100dp" android:layout_height="200dp" android:background="#AA55AA" /></GridLayout>
附:引用聲明
studyboyjlu4的專欄:GridLayout和GridView的區別
《瘋狂Android講義(第二版)》 李剛 《2.2.5 Android4.0新增的網格布局》 電子工業出版社
Android UI之GridLayout(網格布局)