美文网首页
GridLayout 使用

GridLayout 使用

作者: numqin | 来源:发表于2017-11-22 16:37 被阅读76次

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 爸爸,为我们提供了兼容版本。你只需要 :

    1. 引入兼容包:compile 'com.android.support:gridlayout-v7:23.0.0'
    2. 把布局中的 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" 还需要实践

相关文章

  • Android GridLayout动态添加元素

    GridLayout 的基本用法,到处都有教程。学习使用GridLayout 动态添加实现效果 建议使用v7包中的...

  • GridLayout 使用

    GridLayout 使用 GridLayout 是一个强大的网格布局。 基本属性 android:columnC...

  • GridLayout子控件超出屏幕的解决办法

    GridLayout的具体使用方法不赘述,这里主要解决子控件超出屏幕的解决办法,在项目用使用GridLayout的...

  • GridLayout 使用

    GridLayout 网格布局,在开发中并不是很常用,今天偶然用了一下发现并不是太会用,所以作此笔记以避免后面再次...

  • GridLayout 使用

    最近要实现如下效果。根据数据个数不同,进行不同的排列。 3条数据时如下 配置GridLayout 3个数据的情况,...

  • GridLayout 使用总结

    一、简介 GridLayout是Android4.0引入的网格布局,使用它可以减少布局嵌套。也算是常用,但一直没仔...

  • GridLayout使用要点总结

    GridLayout是在Android4.0中引进的新布局,使用它的理由有两个:1,减少布局嵌套层次从而提高性能;...

  • GridLayout网格布局

    网格布局由GridLayout所代表,它是Android4.0新增的布局管理器 GridLayout的基本设置:a...

  • GridLayout

    2 行 3 列的 GridLayout,item 将列平分

  • 使用代码定义GridLayout的方法

    网上关于GridLayout的使用方法基本上都是用的xml文件,但既然是要使用网格布局,那说明准备放入其中的子视图...

网友评论

      本文标题:GridLayout 使用

      本文链接:https://www.haomeiwen.com/subject/abhovxtx.html