Android 中有六大布局

  • LinearLayout(线性布局)
  • RelativeLayout(相对布局)
  • TableLayout(表格布局)
  • FrameLayout(帧布局)
  • AbsoluteLayout(绝对布局)
  • GridLayout(网格布局)

GridLayout(网格布局)

GridLayout 网格布局是在 Android 4.0 以后引入的一种新的布局模式,和表格布局是有点类似的,但比表格布局的好,功能也是很强大的,它可以设置布局有多少行和有多少列,也可以设置布局中的组件的排列方式,也可以设置组件的位置,横跨多少行,多少列。

常用属性

  1. android:orientation 用于设置 vertical 竖直或 horizontal 水平。
  2. android:layout_gravity 为设置对齐方式,可以设置 center,right,left 等。
  3. android:rowCount 为可以设置行数,要多少行设置多少行,如 android:rowCount="2" 为设置网格布局有 2 行。
  4. android:columnCount 为可以设置列数,要多少列设置多少列,如 android:columnCount="2" 为设置网格布局有 2 列。
  5. android:layout_row 为设置组件位于第几行,从 0 开始计数的,如 android:layout_row="1" 为设置组件在第 2 行。
  6. android:layout_column 为设置组件位于第几列,从 0 开始计数的,如 android:layout_column="1" 为设置组件在第 2 列。
  7. android:layout_rowSpan 为设置组件横跨几行,如 android:layout_rowSpan="2" 为纵向横跨 2 行。
  8. android:layout_columnSpan 为设置组件横跨几列,如 android:layout_columnSpan="2" 为橫向横跨 2 列。

注意事项: 低版本 sdk 是如何使用 GridLayout 的呢?要导入 v7 包的 gridlayout 包即可,因为 GirdLayout 是 4.0 后才推出的,标签代码。

<android.support.v7.widget.GridLayout>