前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ArkUI容器类组件-格栅布局容器(GridContainer)

ArkUI容器类组件-格栅布局容器(GridContainer)

原创
作者头像
小帅聊鸿蒙
发布2024-09-28 20:18:31
1020
发布2024-09-28 20:18:31
举报
文章被收录于专栏:鸿蒙开发笔记

ArkUI开发框架提供了纵向排布栅格布局容器 GridContainer ,它仅在栅格布局场景中使用。

GridContainer定义介绍

代码语言:ts
复制
interface GridContainerInterface {
  (value?: GridContainerOptions): GridContainerAttribute;
}

declare interface GridContainerOptions {
  columns?: number | "auto";
  sizeType?: SizeType;
  gutter?: number | string;
  margin?: number | string;
}
  • value:设置布局的可选参数, GridContainerOptions 说明如下:
    • columns:设置当前布局总列数,默认为 auto
    • sizeType:设置选用设备宽度类型, SizeType 参数说明如下:
      • XS:最小宽度类型设备。
      • SM:小宽度类型设备。
      • MD:中等宽度类型设备。
      • LG:大宽度类型设备。
      • Auto(默认值):根据设备类型进行选择。
    • gutter:设置栅格布局列间距。
    • margin:设置栅格布局两侧间距。

简单样例如下所示:

代码语言:ts
复制
    @Entry @Component struct ComponentTest {

      @State sizeType: SizeType = SizeType.XS // 默认采用最小宽度设备类型

      build() {
        Column() {
          GridContainer({
            columns: 12,                      // 设置格栅布局为12列
            sizeType: this.sizeType,          // 设置格栅布局类型
            gutter: 10,                       // 设置格栅布局列间距,该版本还有bug
            margin: 20                        // 设计格栅布局两侧间距
          }) {
            Row() {
              Text('1')
                .useSizeType({
                  xs: { span: 6, offset: 0 }, // Text1在xs设备上占用6列
                  sm: { span: 2, offset: 0 }, // Text1在sm设备上占用2列
                  md: { span: 2, offset: 0 }, // Text1在md设备上占用2列
                  lg: { span: 3, offset: 0 }  // Text1在lg设备上占用3列
                })
                .fontSize(20)
                .height(50)
                .backgroundColor(0x4682B4)
                .textAlign(TextAlign.Center)
              Text('2')
                .useSizeType({
                  xs: { span: 2, offset: 0 }, // Text2在xs设备上占用2列
                  sm: { span: 6, offset: 0 }, // Text2在sm设备上占用6列
                  md: { span: 2, offset: 0 }, // Text2在md设备上占用2列
                  lg: { span: 3, offset: 0 }  // Text2在lg设备上占用3列
                })
                .fontSize(20)
                .height(50)
                .backgroundColor(0x00BFFF)
                .textAlign(TextAlign.Center)
              Text('3')
                .useSizeType({
                  xs: { span: 2, offset: 0 }, // Text3在xs设备上占用2列
                  sm: { span: 2, offset: 0 }, // Text3在sm设备上占用2列
                  md: { span: 6, offset: 0 }, // Text3在md设备上占用6列
                  lg: { span: 3, offset: 0 }  // Text3在lg设备上占用3列
                })
                .fontSize(20)
                .height(50)
                .backgroundColor(0x4682B4)
                .textAlign(TextAlign.Center)
              Text('4')
                .useSizeType({
                  xs: { span: 2, offset: 0 }, // Text4在xs设备上占用2列
                  sm: { span: 2, offset: 0 }, // Text4在sm设备上占用2列
                  md: { span: 2, offset: 0 }, // Text4在md设备上占用2列
                  lg: { span: 3, offset: 0 }  // Text4在lg设备上占用3列
                })
                .fontSize(20)
                .height(50)
                .backgroundColor(0x00BFFF)
                .textAlign(TextAlign.Center)
            }
          }
          .backgroundColor(Color.Pink)
          .width('90%')
          .margin({top: 10})

          Row({space: 10}) {
            Button('XS')
              .onClick(() => {
                this.sizeType = SizeType.XS
              }).backgroundColor(0x317aff)
            Button('SM')
              .onClick(() => {
                this.sizeType = SizeType.SM
              }).backgroundColor(0x317aff)
            Button('MD')
              .onClick(() => {
                this.sizeType = SizeType.MD
              }).backgroundColor(0x317aff)
            Button('LG')
              .onClick(() => {
                this.sizeType = SizeType.LG
              }).backgroundColor(0x317aff)
          }
          .margin({top: 10})
        }.width('100%')
      }
    }

写在最后

如果你觉得这篇内容对你还蛮有帮助,我想邀请你帮我三个小忙:

  • 点赞,转发,有你们的 『点赞和评论』,才是我创造的动力;
  • 关注小编,同时可以期待后续文章ing🚀,不定期分享原创知识;
  • 想要获取更多完整鸿蒙最新学习知识点,可关注B站:码牛课堂鸿蒙开发;

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • GridContainer定义介绍
  • 写在最后
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档