找回密码
 立即注册
首页 业界区 业界 鸿蒙应用开发从入门到实战(二十四):一文搞懂ArkUI网 ...

鸿蒙应用开发从入门到实战(二十四):一文搞懂ArkUI网格布局

赖珊 前天 09:35
大家好,我是潘Sir,持续分享IT技术,帮你少走弯路。《鸿蒙应用开发从入门到项目实战》系列文章持续更新中,陆续更新AI+编程、企业级项目实战等原创内容、欢迎关注!
ArkUI提供了各种布局组件用于界面布局,本文研究使用Grid组件实现网格布局。
一、概述

网格布局(Grid)是一种强大的布局方案,它将页面划分为组成的网格,然后将页面内容在二维网格中进行自由的定位,以下效果都可通过网格布局实现
1.png

网格布局的容器组件为 Grid,子组件为 GridItem,具体语法如下
代码
  1. Grid() {
  2.   GridItem() {
  3.     Text('GridItem')   
  4.   }
  5.   GridItem() {
  6.     Text('GridItem')   
  7.   }
  8.   GridItem() {
  9.     Text('GridItem')   
  10.   }
  11.   GridItem() {
  12.     Text('GridItem')   
  13.   }
  14.   ......
  15. }
复制代码
效果
2.webp

二、常用属性

2.1 划分网格

Grid组件支持自定义行数和列数以及每行和每列的尺寸大小,上述内容需要使用rowsTemplate()方法和columnsTemplate()方法进行设置,具体用法如下
代码
  1. Grid() {
  2.   ForEach([1, 2, 3, 4, 5, 6, 7, 8, 9], (item) => {
  3.     GridItem() {
  4.       Text(item.toString())
  5.         .itemTextStyle()
  6.     }
  7.   })
  8. }
  9. .width(320)
  10. .height(240)
  11. .rowsTemplate('1fr 1fr 1fr')
  12. .columnsTemplate('1fr 2fr 1fr')
  13. .gridStyle()
复制代码
效果
3.webp

说明:
fr为 fraction(比例、分数) 的缩写。fr的个数表示网格布局的行数或列数,fr前面的数值大小,表示该行或列的尺寸占比。
示例代码
pages/layout目录下新建grid目录,新建GridBasic.ets文件
  1. @Entry
  2. @Component
  3. struct GridBasic {
  4.   build() {
  5.     Column() {
  6.       Grid() {
  7.         ForEach([1, 2, 3, 4, 5, 6, 7, 8, 9], (item) => {
  8.           GridItem() {
  9.             Text(item.toString())
  10.               .itemTextStyle12()
  11.           }
  12.         })
  13.       }
  14.       .width(320)
  15.       .height(240)
  16.       .rowsTemplate('1fr 1fr 1fr')
  17.       .columnsTemplate('1fr 2fr 1fr')
  18.       .gridStyle12()
  19.     }.width('100%')
  20.     .height('100%')
  21.     .justifyContent(FlexAlign.Center)
  22.   }
  23. }
  24. @Extend(Text) function itemTextStyle12() {
  25.   .height('100%')
  26.   .width('100%')
  27.   .textAlign(TextAlign.Center)
  28.   .fontColor(Color.White)
  29.   .fontSize(40)
  30.   .fontWeight(FontWeight.Bold)
  31.   .backgroundColor('#008a00')
  32.   .borderWidth(1)
  33. }
  34. @Extend(Grid) function gridStyle12() {
  35.   .backgroundColor('#f5f5f5')
  36.   .borderWidth(1)
  37. }
复制代码
2.2 子组件所占行列数

GridItem组件支持横跨几行或者几列,如下图所示
4.webp

可以使用columnStart()、columnEnd()、rowStart()和rowEnd()方法设置 GridItem 组件所占的单元格,其中rowStart和rowEnd属性表示当前子组件的起始行号和终点行号,columnStart和columnEnd属性表示指定当前子组件的起始列号和终点列号。
说明:
Grid容器中的行号和列号均从0开始。
具体用法如下
代码:
  1. Grid() {
  2.   GridItem() {
  3.     Text('1')
  4.       .itemTextStyle()
  5.   }.rowStart(0).rowEnd(1).columnStart(0).columnEnd(1)
  6.   GridItem() {
  7.     Text('2')
  8.       .itemTextStyle()
  9.   }
  10.   GridItem() {
  11.     Text('3')
  12.       .itemTextStyle()
  13.   }
  14.   GridItem() {
  15.     Text('4')
  16.       .itemTextStyle()
  17.   }
  18.   GridItem() {
  19.     Text('5')
  20.       .itemTextStyle()
  21.   }.columnStart(1).columnEnd(2)
  22. }
  23. .width(320)
  24. .height(240)
  25. .rowsTemplate('1fr 1fr 1fr')
  26. .columnsTemplate('1fr 2fr 1fr')
  27. .gridStyle()
复制代码
效果:
5.webp

示例代码
pages/layout/grid目录,新建StartAndEndPage.ets文件
  1. @Entry
  2. @Component
  3. struct StartAndEndPage {
  4.   build() {
  5.     Column() {
  6.       Grid() {
  7.         GridItem() {
  8.           Text('1')
  9.             .itemTextStyle13()
  10.         }.rowStart(0).rowEnd(1).columnStart(0).columnEnd(1)
  11.         GridItem() {
  12.           Text('2')
  13.             .itemTextStyle13()
  14.         }
  15.         GridItem() {
  16.           Text('3')
  17.             .itemTextStyle13()
  18.         }
  19.         GridItem() {
  20.           Text('4')
  21.             .itemTextStyle13()
  22.         }
  23.         GridItem() {
  24.           Text('5')
  25.             .itemTextStyle13()
  26.         }.columnStart(1).columnEnd(2)
  27.       }
  28.       .width(320)
  29.       .height(240)
  30.       .rowsTemplate('1fr 1fr 1fr')
  31.       .columnsTemplate('1fr 2fr 1fr')
  32.       .gridStyle13()
  33.     }.width('100%')
  34.     .height('100%')
  35.     .justifyContent(FlexAlign.Center)
  36.   }
  37. }
  38. @Extend(Text) function itemTextStyle13() {
  39.   .height('100%')
  40.   .width('100%')
  41.   .textAlign(TextAlign.Center)
  42.   .fontColor(Color.White)
  43.   .fontSize(40)
  44.   .fontWeight(FontWeight.Bold)
  45.   .backgroundColor('#008a00')
  46.   .borderWidth(1)
  47. }
  48. @Extend(Grid) function gridStyle13() {
  49.   .backgroundColor('#f5f5f5')
  50.   .borderWidth(1)
  51. }
复制代码
2.3 行列间距

使用rowsGap()和columnsGap()属性,可以控制行列间距,具体用法如下
代码
  1. Grid() {
  2.   ......
  3. }
  4. .columnsGap(20)
  5. .rowsGap(20)
复制代码
效果
6.webp

示例代码
pages/layout/grid目录,新建GridGap.ets文件
  1. @Entry
  2. @Component
  3. struct GridGap {
  4.   build() {
  5.     Column() {
  6.       Grid() {
  7.         GridItem() {
  8.           Text('1')
  9.             .itemTextStyle14()
  10.         }.rowStart(0).rowEnd(1).columnStart(0).columnEnd(1)
  11.         GridItem() {
  12.           Text('2')
  13.             .itemTextStyle14()
  14.         }.rowStart(0).rowEnd(1)
  15.         GridItem() {
  16.           Text('3')
  17.             .itemTextStyle14()
  18.         }
  19.         GridItem() {
  20.           Text('4')
  21.             .itemTextStyle14()
  22.         }.columnStart(1).columnEnd(2)
  23.       }
  24.       .width(320)
  25.       .height(240)
  26.       .rowsTemplate('1fr 1fr 1fr')
  27.       .columnsTemplate('1fr 2fr 1fr')
  28.       .gridStyle14()
  29.       .rowsGap(20)
  30.       .columnsGap(20)
  31.     }.width('100%')
  32.     .height('100%')
  33.     .justifyContent(FlexAlign.Center)
  34.   }
  35. }
  36. @Extend(Text) function itemTextStyle14() {
  37.   .height('100%')
  38.   .width('100%')
  39.   .textAlign(TextAlign.Center)
  40.   .fontColor(Color.White)
  41.   .fontSize(40)
  42.   .fontWeight(FontWeight.Bold)
  43.   .backgroundColor('#008a00')
  44.   .borderWidth(1)
  45. }
  46. @Extend(Grid) function gridStyle14() {
  47.   .backgroundColor('#f5f5f5')
  48.   .borderWidth(1)
  49. }
复制代码
三、计算器案例

使用网格布局实现如下布局效果
7.webp

示例代码
pages/layout/grid目录,新建CalculatorPage.ets文件
  1. @Entry
  2. @Component
  3. struct CalculatorPage {
  4.   build() {
  5.     Column() {
  6.       Grid() {
  7.         GridItem() {
  8.           Text('0')
  9.             .screenTextStyle()
  10.         }.columnStart(0).columnEnd(3)
  11.         GridItem() {
  12.           Text('CE')
  13.             .buttonTextStyle()
  14.         }
  15.         GridItem() {
  16.           Text('C')
  17.             .buttonTextStyle()
  18.         }
  19.         GridItem() {
  20.           Text('÷')
  21.             .buttonTextStyle()
  22.         }
  23.         GridItem() {
  24.           Text('x')
  25.             .buttonTextStyle()
  26.         }
  27.         GridItem() {
  28.           Text('7')
  29.             .buttonTextStyle()
  30.         }
  31.         GridItem() {
  32.           Text('8')
  33.             .buttonTextStyle()
  34.         }
  35.         GridItem() {
  36.           Text('9')
  37.             .buttonTextStyle()
  38.         }
  39.         GridItem() {
  40.           Text('-')
  41.             .buttonTextStyle()
  42.         }
  43.         GridItem() {
  44.           Text('4')
  45.             .buttonTextStyle()
  46.         }
  47.         GridItem() {
  48.           Text('5')
  49.             .buttonTextStyle()
  50.         }
  51.         GridItem() {
  52.           Text('6')
  53.             .buttonTextStyle()
  54.         }
  55.         GridItem() {
  56.           Text('+')
  57.             .buttonTextStyle()
  58.         }
  59.         GridItem() {
  60.           Text('1')
  61.             .buttonTextStyle()
  62.         }
  63.         GridItem() {
  64.           Text('2')
  65.             .buttonTextStyle()
  66.         }
  67.         GridItem() {
  68.           Text('3')
  69.             .buttonTextStyle()
  70.         }
  71.         GridItem() {
  72.           Text('=')
  73.             .buttonTextStyle()
  74.             .backgroundColor('#1aa1e2')
  75.         }.rowStart(4).rowEnd(5)
  76.         GridItem() {
  77.           Text('0')
  78.             .buttonTextStyle()
  79.         }.columnStart(0).columnEnd(1)
  80.         GridItem() {
  81.           Text('.')
  82.             .buttonTextStyle()
  83.         }
  84.       }
  85.       .gridStyle15()
  86.       .rowsTemplate('1fr 1fr 1fr 1fr 1fr 1fr')
  87.       .columnsTemplate('1fr 1fr 1fr 1fr')
  88.     }.width('100%')
  89.     .height('100%')
  90.     .justifyContent(FlexAlign.Center)
  91.   }
  92. }
  93. @Extend(Text) function screenTextStyle() {
  94.   .backgroundColor('#bac8d3')
  95.   .height('100%')
  96.   .width('100%')
  97.   .textAlign(TextAlign.End)
  98.   .padding(10)
  99.   .borderRadius(10)
  100.   .borderWidth(1)
  101.   .fontSize(40)
  102.   .fontWeight(FontWeight.Bold)
  103. }
  104. @Extend(Text) function buttonTextStyle() {
  105.   .backgroundColor('#f5f5f5')
  106.   .height('100%')
  107.   .width('100%')
  108.   .textAlign(TextAlign.Center)
  109.   .padding(10)
  110.   .borderRadius(10)
  111.   .borderWidth(1)
  112.   .fontSize(25)
  113. }
  114. @Extend(Grid) function gridStyle15() {
  115.   .width(320)
  116.   .height(480)
  117.   .borderRadius(10)
  118.   .borderWidth(1)
  119.   .padding(10)
  120.   .rowsGap(10)
  121.   .columnsGap(10)
  122. }
复制代码
《鸿蒙应用开发从入门到项目实战》系列文章持续更新中,陆续更新AI+编程、企业级项目实战等原创内容,防止迷路,欢迎关注!

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

相关推荐

您需要登录后才可以回帖 登录 | 立即注册