GridLayout 与 TableLayout 用法非常相似,不过它的“单元格”可以跨行,没有了 Row 子元素,代码更简洁,还有据说它渲染速度比 TableLayout 快。
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:rowCount="3"
android:columnCount="3">
android:text="行1列1" />
android:text="行1列2" />
android:text="跨行"
android:layout_rowSpan="2"
android:layout_gravity="fill" />
android:text="跨列"
android:layout_columnSpan="2"
android:layout_gravity="fill" />
android:text="行3列2"
android:layout_row="2"
android:layout_column="1" />
效果如下:
GridLayout 核心属性:rowCount、columnCount
GridLayout 没有像 TableLayout 一样跟 TableRow,而是直接跟控件。
“单元格”核心属性:layout_rowSpan、layout_columnSpan、layout_gravity、layout_row、layout_column。
注意:GridLayout 的 layout_width 为 wrap_content,否则里面 layout_gravity="fill" 的元素可能不会与周边的元素对齐,它可能会宽些或高些。