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

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

GridContainer定义介绍

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:设置栅格布局两侧间距。

简单样例如下所示:

    @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%')
      }
    }
阅读全文
下载说明:
1、本站所有资源均从互联网上收集整理而来,仅供学习交流之用,因此不包含技术服务请大家谅解!
2、本站不提供任何实质性的付费和支付资源,所有需要积分下载的资源均为网站运营赞助费用或者线下劳务费用!
3、本站所有资源仅用于学习及研究使用,您必须在下载后的24小时内删除所下载资源,切勿用于商业用途,否则由此引发的法律纠纷及连带责任本站和发布者概不承担!
4、本站站内提供的所有可下载资源,本站保证未做任何负面改动(不包含修复bug和完善功能等正面优化或二次开发),但本站不保证资源的准确性、安全性和完整性,用户下载后自行斟酌,我们以交流学习为目的,并不是所有的源码都100%无错或无bug!如有链接无法下载、失效或广告,请联系客服处理!
5、本站资源除标明原创外均来自网络整理,版权归原作者或本站特约原创作者所有,如侵犯到您的合法权益,请立即告知本站,本站将及时予与删除并致以最深的歉意!
6、如果您也有好的资源或教程,您可以投稿发布,成功分享后有站币奖励和额外收入!
7、如果您喜欢该资源,请支持官方正版资源,以得到更好的正版服务!
8、请您认真阅读上述内容,注册本站用户或下载本站资源即您同意上述内容!
原文链接:https://www.1024c.cn/archives/21110,转载请注明出处。
0

评论0

显示验证码
没有账号?注册  忘记密码?