鸿蒙HarmonyOS实战-ArkUI组件(GridRow GridCol)_gridview 鸿蒙

1.概述

栅格布局是一种通用的辅助定位工具,可以帮助开发人员解决多尺寸多设备的动态布局问题。通过将页面划分为等宽的列数和行数,栅格布局提供了可循的规律性结构,方便开发人员对页面元素进行定位和排版。此外,栅格布局还提供了一种统一的定位标注,帮助保证不同设备上各个模块的布局一致性,减少设计和开发的复杂度,提高工作效率。栅格布局还具有灵活的间距调整方法,可以满足特殊场景布局调整的需求。同时,自动换行和自适应功能使得栅格布局能够完成一对多布局,并自动适应不同设备上的排版。在栅格布局中,栅格容器组件GridRow与栅格子组件GridCol需要联合使用,共同构建出栅格布局场景。

2.栅格容器GridRow

2.1 栅格系统断点
断点名称设备描述
xs最小宽度类型设备
sm小宽度类型设备
md中等宽度类型设备
lg大宽度类型设备
xl特大宽度类型设备
xxl超大宽度类型设备

在GridRow栅格组件中,开发者可以使用breakpoints自定义修改断点的取值范围,最多支持6个断点。除了默认的四个断点以外,还可以启用xl,xxl两个断点,支持六种不同尺寸(xs, sm, md, lg, xl, xxl)设备的布局设置。

定于如下:

breakpoints: {
  value: ['200vp', '300vp', '400vp', '500vp', '600vp'],
  reference: BreakpointsReference.WindowSize
}

案例如下:

@Entry
@Component
struct Index {
  @State bgColors: Color[] = [Color.Red, Color.Orange, Color.Yellow, Color.Green, Color.Pink, Color.Grey, Color.Blue, Color.Brown];
  build() {
    GridRow({
      breakpoints: {
        value: ['200vp', '300vp', '400vp', '500vp', '600vp'],
        reference: BreakpointsReference.WindowSize //断点切换参考物
      }
    }) {
      ForEach(this.bgColors, (color, index) => {
        GridCol({
          span: {
            xs: 2, // 在最小宽度类型设备上,栅格子组件占据的栅格容器2列。
            sm: 3, // 在小宽度类型设备上,栅格子组件占据的栅格容器3列。
            md: 4, // 在中等宽度类型设备上,栅格子组件占据的栅格容器4列。
            lg: 6, // 在大宽度类型设备上,栅格子组件占据的栅格容器6列。
            xl: 8, // 在特大宽度类型设备上,栅格子组件占据的栅格容器8列。
            xxl: 12 // 在超大宽度类型设备上,栅格子组件占据的栅格容器12列。
          }
        }) {
          Row() {
            Text(`${index}`)
          }.width("100%").height('50vp')
        }.backgroundColor(color)
      })
    }
  }
}

在这里插入图片描述

2.2 布局的总列数

栅格布局的列数是指将页面宽度分为多少等分,一般情况下栅格布局的列数为12列,即将页面宽度分为12等分,每列所占宽度相等。这样可以方便地将页面元素放置到网格系统中,达到快速搭建页面的目的。同时,栅格布局的列数也可以根据具体的需求进行调整,并不一定非要是12列。

1、默认列数

columns默认值为12,即在未设置columns时,任何断点下,栅格布局被分成12列。

@Entry
@Component
struct Index {
  @State bgColors: Color[] = [Color.Red, Color.Orange, Color.Yellow, Color.Green, Color.Pink, Color.Grey, Color.Blue, Color.Brown];
  build() {
    GridRow() {
      ForEach(this.bgColors, (item, index) => {
        GridCol() {
          Row() {
            Text(`${index + 1}`)
          }.width('100%').height('50')
        }.backgroundColor(item)
      })
    }
  }
}         

在这里插入图片描述
2、设置列数

当columns为自定义值,栅格布局在任何尺寸设备下都被分为columns列。

@Entry
@Component
struct Index {
  @State bgColors: Color[] = [Color.Red, Color.Orange, Color.Yellow, Color.Green, Color.Pink, Color.Grey, Color.Blue, Color.Brown];
  @State currentBp: string = 'unknown';
  build() {
    Row() {
      GridRow({ columns: 4 }) {
        ForEach(this.bgColors, (item, index) => {
          GridCol() {
            Row() {
              Text(`${index + 1}`)
            }.width('100%').height('50')
          }.backgroundColor(item)
        })
      }
      .width('100%').height('100%')
      .onBreakpointChange((breakpoint) => {
        this.currentBp = breakpoint
      })
    }
    .height(160)
    .border({ color: Color.Blue, width: 2 })
    .width('100%')
  }
}

在这里插入图片描述
3、公用

@Entry
@Component
struct Index {
  @State bgColors: Color[] = [Color.Red, Color.Orange, Color.Yellow, Color.Green, Color.Pink, Color.Grey, Color.Blue, Color.Brown];
  @State currentBp: string = 'unknown';
  build() {
    GridRow({ columns: { sm: 4, md: 8 }, breakpoints: { value: ['200vp', '300vp', '400vp', '500vp', '600vp'] } }) {
      ForEach(this.bgColors, (item, index) => {
        GridCol() {
          Row() {
            Text(`${index + 1}`)
          }.width('100%').height('50')
        }.backgroundColor(item)
      })
    }
  }
}

在这里插入图片描述

若只设置sm, md的栅格总列数,则较小的尺寸使用默认columns值12,较大的尺寸使用前一个尺寸的columns。这里只设置sm:4, md:8,则较小尺寸的xs:12,较大尺寸的参照md的设置,lg:8, xl:8, xxl:8。

2.3 排列方向

可以通过设置GridRow的direction属性来指定栅格子组件在栅格容器中的排列方向。

该属性可以设置为

  • GridRowDirection.Row(从左往右排列)
  • GridRowDirection.RowReverse(从右往左排列)
@Entry
@Component
struct Index {
  @State bgColors: Color[] = [Color.Red, Color.Orange, Color.Yellow, Color.Green, Color.Pink, Color.Grey, Color.Blue, Color.Brown];
  @State currentBp: string = 'unknown';
  build() {
    GridRow({ columns: { sm: 4, md: 8 }, breakpoints: { value: ['200vp', '300vp', '400vp', '500vp', '600vp'] } ,direction: GridRowDirection.RowReverse}) {
      ForEach(this.bgColors, (item, index) => {
        GridCol() {
          Row() {
            Text(`${index + 1}`)
          }.width('100%').height('50')
        }.backgroundColor(item)
      })
    }
  }
}

在这里插入图片描述

2.4 子组件间距

可以通过设置GridRow的gutter属性来指定栅格子组件在栅格容器中的排列方向。

该属性可以设置为

  • 单个值GridRow({ gutter: 10 }){}(X:10,Y:10)
  • 多个值GridRow({ gutter: { x: 20, y: 50 } }){}(X:20,Y:50)
@Entry
@Component
struct Index {
  @State bgColors: Color[] = [Color.Red, Color.Orange, Color.Yellow, Color.Green, Color.Pink, Color.Grey, Color.Blue, Color.Brown];
  @State currentBp: string = 'unknown';
  build() {
    GridRow({ columns: { sm: 4}, breakpoints: { value: ['200vp', '300vp', '400vp', '500vp', '600vp'] } ,direction: GridRowDirection.RowReverse,gutter: { x: 20, y: 50 }}) {
      ForEach(this.bgColors, (item, index) => {
        GridCol() {
          Row() {
            Text(`${index + 1}`)
          }.width('100%').height('50')
        }.backgroundColor(item)
      })
    }
  }
}

在这里插入图片描述

3.子组件GridCol

GridCol组件作为GridRow组件的子组件,通过给GridCol传参或者设置属性两种方式,设置span(占用列数),offset(偏移列数),order(元素序号)的值。

设置span。

GridCol({ span: 2 }){}
GridCol({ span: { xs: 1, sm: 2, md: 3, lg: 4 } }){}
GridCol(){}.span(2)
GridCol(){}.span({ xs: 1, sm: 2, md: 3, lg: 4 })

设置offset。

GridCol({ offset: 2 }){}
GridCol({ offset: { xs: 2, sm: 2, md: 2, lg: 2 } }){}
GridCol(){}.offset(2)
GridCol(){}.offset({ xs: 1, sm: 2, md: 3, lg: 4 }) 

设置order。

GridCol({ order: 2 }){}
GridCol({ order: { xs: 1, sm: 2, md: 3, lg: 4 } }){}
GridCol(){}.order(2)
GridCol(){}.order({ xs: 1, sm: 2, md: 3, lg: 4 })

3.1 span

在栅格布局中,span属性表示某个元素或组件应该跨越的列数。例如,如果我们有一个包含12个列的栅格系统,我们可以使用span属性来指定一个元素应该占用多少列。

例如,如果我们希望一个元素占用3列,我们可以使用{ span: 3 },这将使元素跨越3列。同样,如果我们希望元素跨越整个栅格系统,我们可以使用{ span: 12 }

在具体的代码实现中,不同的栅格系统可能会使用不同的方式实现span属性,但通常都会提供类似于{ span: 3 }的语法来指定元素所占据的列数。

1、当类型为number时,子组件在所有尺寸设备下占用的列数相同。

@Entry
@Component
struct Index {
  @State bgColors: Color[] = [Color.Red, Color.Orange, Color.Yellow, Color.Green, Color.Pink, Color.Grey, Color.Blue, Color.Brown];
  @State currentBp: string = 'unknown';
  build() {
    GridRow({ columns: 8 }) {
      ForEach(this.bgColors, (color, index) => {
        GridCol({ span: 2 }) {
          Row() {
            Text(`${index}`)
          }.width('100%').height('50vp')
        }
        .backgroundColor(color)
      })
    }
  }
}

在这里插入图片描述
2、当类型为GridColColumnOption时,支持六种不同尺寸(xs, sm, md, lg, xl, xxl)设备中子组件所占列数设置,各个尺寸下数值可不同。

@State bgColors: Color[] = [Color.Red, Color.Orange, Color.Yellow, Color.Green, Color.Pink, Color.Grey, Color.Blue, Color.Brown];
...
GridRow({ columns: 8 }) {
  ForEach(this.bgColors, (color, index) => {
    GridCol({ span: { xs: 3, sm: 3, md: 3, lg: 3 } }) {      
      Row() {
        Text(`${index}`)
      }.width('100%').height('50vp')          
    }
    .backgroundColor(color)
  })
}                

在这里插入图片描述

3.2 offset

栅格布局的offset是指在栅格布局中,元素相对于其父元素的偏移量。使用offset可以将元素移动到栅格中的任意位置。在栅格布局中,通常使用偏移量来实现布局的灵活和自适应性。具体的偏移量取决于栅格的列数和元素所占的列数。通常,一般情况下,偏移量是通过给元素添加相应的类来实现的,例如:{ offset: 3 }表示元素在中等屏幕上向右偏移3个栅格。

1、当类型为number时,子组件偏移相同列数

@Entry
@Component
struct Index {
  @State bgColors: Color[] = [Color.Red, Color.Orange, Color.Yellow, Color.Green, Color.Pink, Color.Grey, Color.Blue, Color.Brown];
  @State currentBp: string = 'unknown';
  build() {
    GridRow({ columns: 8 }) {
      ForEach(this.bgColors, (color, index) => {
        GridCol({ offset: 2 }) {
          Row() {
            Text(`${index}`)
          }.width('100%').height('50vp')
        }
        .backgroundColor(color)
      })
    }
  }
}

在这里插入图片描述
2、当类型为GridColColumnOption时,支持六种不同尺寸(xs, sm, md, lg, xl, xxl)设备中子组件所占列数设置,各个尺寸下数值可不同

@Entry
@Component
struct Index {
  @State bgColors: Color[] = [Color.Red, Color.Orange, Color.Yellow, Color.Green, Color.Pink, Color.Grey, Color.Blue, Color.Brown];
  @State currentBp: string = 'unknown';
  build() {
    GridRow({ columns: 8 }) {


**自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。**

**深知大多数HarmonyOS鸿蒙开发工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!**

**因此收集整理了一份《2024年HarmonyOS鸿蒙开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。**
![img](https://img-blog.csdnimg.cn/img_convert/c5e8cdb3b2b7b931ba281bb6dc4c1d71.png)
![img](https://img-blog.csdnimg.cn/img_convert/5cd94bd47987c6f6c85d6f12986ddb8b.png)
![img](https://img-blog.csdnimg.cn/img_convert/b781951559076bb6522b222de47fef9c.png)

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上HarmonyOS鸿蒙开发知识点,真正体系化!**

**由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新**

**如果你觉得这些内容对你有帮助,可以添加VX:vip204888 (备注鸿蒙获取)**
![img](https://img-blog.csdnimg.cn/img_convert/cf07b6ae6dfb33c58bf76fca1523ea5a.png)

**一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

中...(img-i88WLUo0-1712918621342)]
[外链图片转存中...(img-mQWUiEGN-1712918621342)]

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上HarmonyOS鸿蒙开发知识点,真正体系化!**

**由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新**

**如果你觉得这些内容对你有帮助,可以添加VX:vip204888 (备注鸿蒙获取)**
[外链图片转存中...(img-3e2tYOmK-1712918621343)]

**一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值