GridLayout 使用
GridLayout 是一個強大的網格布局。
基本屬性
- android:columnCount 整數,最多的列數
- android:rowCount 整數,最多的行數
根據上面兩參數,GridLayout 將布局劃分為 columnCount 列、rowCount 行的網格布局
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnCount="3"
android:rowCount="2">
</GridLayout>
上面既是將 GridLayout 設置為一個三列、兩行的網格。每一個單元格,是 GridLayout 占用的最小單位。
單元格位置控制
- android:layout_column 整數n,在哪一列開始顯示n=[0, 最大列-1]
- android:layout_columnSpan 整數k,指定元素橫跨幾列,需要注意保證n+k <= 最大列數
- android:layout_row 指定從哪一行開始顯示,規則同列數
- android:layout_rowSpan 縱向跨幾行,規則同列
上面的四個屬性可以控制單元格的顯示位置,及跨行顯示時占用的單元格
注意:如果一個單元格沒有指定希望占據的單元格的行和列索引,GridLayout將使用它的orientation,rowCount和columnCount屬性自動分配單元格的位置。
- 設置位置
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnCount="2"
android:rowCount="2">
<Button
android:layout_column="0"
android:layout_row="0"
android:text="1" />
<Button
android:layout_column="1"
android:layout_row="0"
android:text="2" />
<Button
android:layout_column="0"
android:layout_row="1"
android:text="3" />
<Button
android:layout_column="1"
android:layout_row="1"
android:text="4" />
</GridLayout>
-
設置跨行、跨列顯示
跨行、跨列顯示
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:alignmentMode="alignMargins"
android:columnCount="3"
android:rowCount="4">
<Button
android:layout_gravity="fill_vertical|fill_horizontal"
android:text="2" />
<Button
android:layout_gravity="fill_vertical|fill_horizontal"
android:text="3" />
<Button
android:layout_gravity="fill_vertical|fill_horizontal"
android:text="1" />
<Button
android:layout_columnSpan="2"
android:layout_gravity="fill_vertical|fill_horizontal"
android:layout_rowSpan="2"
android:text="1" />
<Button
android:layout_gravity="fill_vertical|fill_horizontal"
android:text="1" />
<Button
android:layout_gravity="fill_vertical|fill_horizontal"
android:text="1" />
</GridLayout>
單元格的填充方式
- center
- center_horizontal
- center_vertical
- top
- left
- bottom
- right
- start
- end
- fill
- fill_vertical
- fill_horizontal
- clip_vertical
- clip_horizontal
使用的話就像這樣
android:layout_gravity="fill_vertical|fill_horizontal"
單元格的比重
-
在 android 版本 21 以上加入了比重這個屬性
- android:layout_columnWeight="1"
- android:layout_rowWeight="1"
在使用上跟 Linearlayout 類似。
之前也說了是 android 版本 21 以上,如果你應用的 minSdkVersion 小于 21 ,不好意思是不能用的。但google 爸爸,為我們提供了兼容版本。你只需要 :
- 引入兼容包:compile 'com.android.support:gridlayout-v7:23.0.0'
- 把布局中的 GridLayout 替換成 android.support.v7.widget.GridLayout 即可
不過值得注意的是官方文檔有這個一句
Interpretation of GONE
For layout purposes, GridLayout treats views whose visibility status is GONE
, as having zero width and height. This is subtly different from the policy of ignoring views that are marked as GONE outright. If, for example, a gone-marked view was alone in a column, that column would itself collapse to zero width if and only if no gravity was defined on the view. If gravity was defined, then the gone-marked view has no effect on the layout and the container should be laid out as if the view had never been added to it. GONE views are taken to have zero weight during excess space distribution.
These statements apply equally to rows as well as columns, and to groups of rows or columns
能力有限無法理解,關于單元格的 android:visibility="gone" 還需要實踐
?