【ArkUI】Grid布局

Grid组件简介

Grid组件为网格容器,是一种网格列表,由「行」和「列」分割的单元格所组成,通过指定「项目」所在的单元格做出各种各样的布局。
Grid组件一般和子组件GridItem一起使用,Grid列表中的每一个条目对应一个GridItem组件。

和List组件一样,Grid组件也可以使用ForEach来渲染多个列表项GridItem。
通过下面这段示例代码来介绍Grid组件的使用。

@Entry
@Component
struct GridExample {
  // 定义一个长度为16的数组
  private arr: string[] = new Array(16).fill('').map((_, index) => `item ${index}`);

  build() {
    Column() {
      Grid() {
        ForEach(
          this.arr,
          (item: string) => {
            GridItem() {
              Text(item)
                .fontSize(16)
                .fontColor(Color.White)
                .backgroundColor(0x007DFF)
                .width('100%')
                .height('100%')
                .textAlign(TextAlign.Center)
          }
        }, item => item)
      }
      .scrollBar(BarState.On)  // 开启滚动条
      .columnsTemplate('1fr 2fr 1fr 1fr')  // 列数,且每列占比
      .rowsTemplate('1fr 2fr 1fr 1fr')  // 行数,且每行占比
      .columnsGap(10)   // 列间距
      .rowsGap(30)  // 行间距
      .width("100%")
      .height(500)
    }
    .width('100%')
    .padding(12)
    .backgroundColor(0xF1F3F5)
  }
}
  • 示例代码中创建了16个GridItem列表项。
  • 设置columnsTemplate的值为’1fr 2fr 1fr 1fr’,表示这个网格为4列,将Grid允许的宽分为5等分,第二列占2份,其余每列占1份。
  • rowsTemplate的值为’1fr 2fr 1fr 1fr’,表示这个网格为4行,将Grid允许的高分为5等分,第二行占2份,其余每行占1份。
  • 这样就构成了一个4行4列的网格列表。
  • 使用columnsGap设置列间距为10vp,使用rowsGap设置行间距为30vp。

效果如下
在这里插入图片描述

上述代码使用了固定的行数和列数,所以构建出的网格是不可滚动的。
然而有时候因为内容较多,我们通过滚动的方式来显示更多的内容,就需要一个可以滚动的网格布局。
只需要设置rowsTemplate和columnsTemplate中的一个即可,比如想实现竖向滚动条,设置columnsTemplate并给每个GridItem设置height即可。

@Entry
@Component
struct GridExample {
  // 定义一个长度为16的数组
  private arr: string[] = new Array(16).fill('').map((_, index) => `item ${index}`);

  build() {
    Scroll(){
      Column({space:60}) {
        // 网格布局使用了固定的行数和列数,所以构建出的网格是不可滚动的。
        // 然而有时候因为内容较多,我们通过滚动的方式来显示更多的内容,就需要一个可以滚动的网格布局。
        // 我们只需要设置rowsTemplate和columnsTemplate中的一个即可。
        Grid() {
          ForEach(
            this.arr,
            (item: string) => {
              GridItem() {
                Text(item)
                  .fontSize(16)
                  .fontColor(Color.White)
                  .backgroundColor(0x007DFF)
                  .width('100%')
                  .height('100%')
                  .textAlign(TextAlign.Center)
              }
            }, item => item)
        }
        .scrollBar(BarState.On)  // 开启滚动条
        .columnsTemplate('1fr 2fr 1fr 1fr')  // 列数,且每列占比
        .rowsTemplate('1fr 2fr 1fr 1fr')  // 行数,且每行占比
        .columnsGap(10)   // 列间距
        .rowsGap(30)  // 行间距
        .width("100%")
        .height(500)

        // 将示例代码中GridItem的高度设置为固定值,例如100
        // 仅设置columnsTemplate属性,不设置rowsTemplate属性,就可以实现Grid列表的滚动
        Grid() {
          ForEach(this.arr, (item: string) => {
            GridItem() {
              Text(item)
                .height(100)
                .fontSize(16)
                .fontColor(Color.White)
                .backgroundColor(0x007DFF)
                .width('100%')
                .textAlign(TextAlign.Center)
            }
          }, item => item)
        }
        .scrollBar(BarState.On)  // 开启滚动条
        .columnsTemplate('1fr 1fr 1fr')
        .columnsGap(10)
        .rowsGap(10)
        .height(300)

        Grid() {
          ForEach(this.arr, (item: string) => {
            GridItem() {
              Text(item)
                .width(100)
                .fontSize(16)
                .fontColor(Color.White)
                .backgroundColor(0x007DFF)
                .height('100%')
                .textAlign(TextAlign.Center)
            }
          }, item => item)
        }
        .scrollBar(BarState.On)  // 开启滚动条
        .rowsTemplate('1fr 1fr 1fr')
        .columnsGap(10)
        .rowsGap(10)
        .height(300)
      }
      .width('100%')
      .padding(12)
      .backgroundColor(0xF1F3F5)
    }
  }
}
  • 10
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

田本初

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值