当前位置:   article > 正文

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

gridview 鸿蒙

 一、GridRow/GridCol

1.概述

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

2.栅格容器GridRow

2.1 栅格系统断点

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

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

定于如下:

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

案例如下:

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

在这里插入图片描述

2.2 布局的总列数

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

1、默认列数

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

  1. @Entry
  2. @Component
  3. struct Index {
  4. @State bgColors: Color[] = [Color.Red, Color.Orange, Color.Yellow, Color.Green, Color.Pink, Color.Grey, Color.Blue, Color.Brown];
  5. build() {
  6. GridRow() {
  7. ForEach(this.bgColors, (item, index) => {
  8. GridCol() {
  9. Row() {
  10. Text(`${index + 1}`)
  11. }.width('100%').height('50')
  12. }.backgroundColor(item)
  13. })
  14. }
  15. }
  16. }

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

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

  1. @Entry
  2. @Component
  3. struct Index {
  4. @State bgColors: Color[] = [Color.Red, Color.Orange, Color.Yellow, Color.Green, Color.Pink, Color.Grey, Color.Blue, Color.Brown];
  5. @State currentBp: string = 'unknown';
  6. build() {
  7. Row() {
  8. GridRow({ columns: 4 }) {
  9. ForEach(this.bgColors, (item, index) => {
  10. GridCol() {
  11. Row() {
  12. Text(`${index + 1}`)
  13. }.width('100%').height('50')
  14. }.backgroundColor(item)
  15. })
  16. }
  17. .width('100%').height('100%')
  18. .onBreakpointChange((breakpoint) => {
  19. this.currentBp = breakpoint
  20. })
  21. }
  22. .height(160)
  23. .border({ color: Color.Blue, width: 2 })
  24. .width('100%')
  25. }
  26. }

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

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

在这里插入图片描述

若只设置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(从右往左排列)
  1. @Entry
  2. @Component
  3. struct Index {
  4. @State bgColors: Color[] = [Color.Red, Color.Orange, Color.Yellow, Color.Green, Color.Pink, Color.Grey, Color.Blue, Color.Brown];
  5. @State currentBp: string = 'unknown';
  6. build() {
  7. GridRow({ columns: { sm: 4, md: 8 }, breakpoints: { value: ['200vp', '300vp', '400vp', '500vp', '600vp'] } ,direction: GridRowDirection.RowReverse}) {
  8. ForEach(this.bgColors, (item, index) => {
  9. GridCol() {
  10. Row() {
  11. Text(`${index + 1}`)
  12. }.width('100%').height('50')
  13. }.backgroundColor(item)
  14. })
  15. }
  16. }
  17. }

在这里插入图片描述

2.4 子组件间距

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

该属性可以设置为

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

在这里插入图片描述

3.子组件GridCol

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

设置span。

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

设置offset。

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

设置order。

  1. GridCol({ order: 2 }){}
  2. GridCol({ order: { xs: 1, sm: 2, md: 3, lg: 4 } }){}
  3. GridCol(){}.order(2)
  4. 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时,子组件在所有尺寸设备下占用的列数相同。

  1. @Entry
  2. @Component
  3. struct Index {
  4. @State bgColors: Color[] = [Color.Red, Color.Orange, Color.Yellow, Color.Green, Color.Pink, Color.Grey, Color.Blue, Color.Brown];
  5. @State currentBp: string = 'unknown';
  6. build() {
  7. GridRow({ columns: 8 }) {
  8. ForEach(this.bgColors, (color, index) => {
  9. GridCol({ span: 2 }) {
  10. Row() {
  11. Text(`${index}`)
  12. }.width('100%').height('50vp')
  13. }
  14. .backgroundColor(color)
  15. })
  16. }
  17. }
  18. }

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

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

在这里插入图片描述

3.2 offset

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

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

  1. @Entry
  2. @Component
  3. struct Index {
  4. @State bgColors: Color[] = [Color.Red, Color.Orange, Color.Yellow, Color.Green, Color.Pink, Color.Grey, Color.Blue, Color.Brown];
  5. @State currentBp: string = 'unknown';
  6. build() {
  7. GridRow({ columns: 8 }) {
  8. ForEach(this.bgColors, (color, index) => {
  9. GridCol({ offset: 2 }) {
  10. Row() {
  11. Text(`${index}`)
  12. }.width('100%').height('50vp')
  13. }
  14. .backgroundColor(color)
  15. })
  16. }
  17. }
  18. }

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

  1. @Entry
  2. @Component
  3. struct Index {
  4. @State bgColors: Color[] = [Color.Red, Color.Orange, Color.Yellow, Color.Green, Color.Pink, Color.Grey, Color.Blue, Color.Brown];
  5. @State currentBp: string = 'unknown';
  6. build() {
  7. GridRow({ columns: 8 }) {
  8. ForEach(this.bgColors, (color, index) => {
  9. GridCol({ offset: { xs: 1, sm: 3, md: 2, lg: 4 } }) {
  10. Row() {
  11. Text(`${index}`)
  12. }.width('100%').height('50vp')
  13. }
  14. .backgroundColor(color)
  15. })
  16. }
  17. }
  18. }

在这里插入图片描述

3.3 order

栅格布局中,order属性用于指定网格项的显示顺序。默认情况下,网格项的显示顺序是按照它们在 HTML 代码中出现的顺序进行排列。通过设置 order 属性,可以改变网格项的显示顺序。

order 属性的值是一个整数,表示网格项的显示顺序。值越小的网格项越先显示,值相同的网格项按照它们在 HTML 代码中的顺序进行排列。若未设置该属性,则默认值为 0。

1、当类型为number时,子组件在任何尺寸下排序次序一致。

  1. @Entry
  2. @Component
  3. struct Index {
  4. @State bgColors: Color[] = [Color.Red, Color.Orange, Color.Yellow, Color.Green, Color.Pink, Color.Grey, Color.Blue, Color.Brown];
  5. @State currentBp: string = 'unknown';
  6. build() {
  7. GridRow() {
  8. GridCol({ order: 4 }) {
  9. Row() {
  10. Text('1')
  11. }.width('100%').height('50vp')
  12. }.backgroundColor(Color.Red)
  13. GridCol({ order: 3 }) {
  14. Row() {
  15. Text('2')
  16. }.width('100%').height('50vp')
  17. }.backgroundColor(Color.Orange)
  18. GridCol({ order: 2 }) {
  19. Row() {
  20. Text('3')
  21. }.width('100%').height('50vp')
  22. }.backgroundColor(Color.Yellow)
  23. GridCol({ order: 1 }) {
  24. Row() {
  25. Text('4')
  26. }.width('100%').height('50vp')
  27. }.backgroundColor(Color.Green)
  28. }
  29. }
  30. }

在这里插入图片描述

2、当类型为GridColColumnOption时,支持六种不同尺寸(xs, sm, md, lg, xl, xxl)设备中子组件排序次序设置。在xs设备中,子组件排列顺序为1234;sm为2341,md为3412,lg为2431。

  1. @Entry
  2. @Component
  3. struct Index {
  4. @State bgColors: Color[] = [Color.Red, Color.Orange, Color.Yellow, Color.Green, Color.Pink, Color.Grey, Color.Blue, Color.Brown];
  5. @State currentBp: string = 'unknown';
  6. build() {
  7. GridRow() {
  8. GridCol({ order: { xs:1, sm:5, md:3, lg:7}}) {
  9. Row() {
  10. Text('1')
  11. }.width('100%').height('50vp')
  12. }.backgroundColor(Color.Red)
  13. GridCol({ order: { xs:2, sm:2, md:6, lg:1} }) {
  14. Row() {
  15. Text('2')
  16. }.width('100%').height('50vp')
  17. }.backgroundColor(Color.Orange)
  18. GridCol({ order: { xs:3, sm:3, md:1, lg:6} }) {
  19. Row() {
  20. Text('3')
  21. }.width('100%').height('50vp')
  22. }.backgroundColor(Color.Yellow)
  23. GridCol({ order: { xs:4, sm:4, md:2, lg:5} }) {
  24. Row() {
  25. Text('4')
  26. }.width('100%').height('50vp')
  27. }.backgroundColor(Color.Green)
  28. }
  29. }
  30. }

在这里插入图片描述

4.栅格组件的嵌套使用

  1. @Entry
  2. @Component
  3. struct Index {
  4. @State bgColors: Color[] = [Color.Red, Color.Orange, Color.Yellow, Color.Green, Color.Pink, Color.Grey, Color.Blue, Color.Brown];
  5. @State currentBp: string = 'unknown';
  6. build() {
  7. GridRow() {
  8. GridCol({ span: { sm: 12 } }) {
  9. GridRow() {
  10. GridCol({ span: { sm: 2 } }) {
  11. Row() {
  12. Text('left').fontSize(24)
  13. }
  14. .justifyContent(FlexAlign.Center)
  15. .height('90%')
  16. }.backgroundColor('#ff41dbaa')
  17. GridCol({ span: { sm: 10 } }) {
  18. Row() {
  19. Text('right').fontSize(24)
  20. }
  21. .justifyContent(FlexAlign.Center)
  22. .height('90%')
  23. }.backgroundColor('#ff4168db')
  24. }
  25. .backgroundColor('#19000000')
  26. .height('100%')
  27. }
  28. GridCol({ span: { sm: 12 } }) {
  29. Row() {
  30. Text('footer').width('100%').textAlign(TextAlign.Center)
  31. }.width('100%').height('10%').backgroundColor(Color.Pink)
  32. }
  33. }.width('100%').height(300)
  34. }
  35. }

在这里插入图片描述

 声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】

推荐阅读
相关标签