鸿蒙基本UI控件(List相关-含Grid)

项目创建传送门:

鸿蒙基本UI控件(Text相关-含项目创建流程)https://blog.csdn.net/qq_39731011/article/details/157513416?spm=1001.2014.3001.5501

1、代码示例:pages目录下的Index.ets

TypeScript 复制代码
@Entry
@Component
struct Index {
  @State listData: string[] = ['数据0', '数据1', '数据2', '数据3'];

  build() {
    Row() { //水平居中
      Column() { //垂直居中
        //-------------------------------------列表相关---------------------------------
        List({ space: 10 }) {
          ForEach(this.listData, (item: string) => {
            ListItem() {
              Text(item)
                .fontSize(20)
                .width('100%')
                .padding(8)
                .backgroundColor($r('app.color.listBack'))
                .borderRadius(10)
            }

          }, (item: string) => item) // 防止重新渲染
        }.margin(10)

        Grid() {
          GridItem() {
            Text(this.listData[0])
              .fontSize(20)
              .width('100%')
              .height('100%')
              .padding(8)
              .backgroundColor($r('app.color.gridBack'))
              .borderRadius(10)
          }.rowStart(1).rowEnd(2) //占两行
          GridItem() {
            Text(this.listData[1])
              .fontSize(20)
              .width('100%')
              .height('100%')
              .padding(8)
              .backgroundColor($r('app.color.gridBack'))
              .borderRadius(10)
          }.columnStart(1).columnEnd(2) //占两列
          GridItem() {
            Text(this.listData[2])
              .fontSize(20)
              .width('100%')
              .height('100%')
              .padding(8)
              .backgroundColor($r('app.color.gridBack'))
              .borderRadius(10)
          }

          GridItem() {
            Text(this.listData[3])
              .fontSize(20)
              .width('100%')
              .height('100%')
              .padding(8)
              .backgroundColor($r('app.color.gridBack'))
              .borderRadius(10)
          }
        }
        .rowsTemplate('1fr 1fr 1fr') //行数
        .rowsGap(8) //行间距
        .columnsTemplate('1fr 2fr 1fr') //列数
        .columnsGap(8) //列间距
        .margin(10)
        .height(200)
      }
      .width('100%')
    }
    .height('100%')
  }
}

2、运行结果:

相关推荐
woshihuanglaoshi1 小时前
鸿蒙技术进阶全景高级成长体系:从初中级到架构师的技能树/学习路径/项目实战/认证体系系统性方法论
学习·华为·harmonyos
轻口味2 小时前
端侧 AI 赋能鸿蒙版 Obsidian:轻规划基于 HarmonyOS 7.0 图像超分特性的体验突破与开发实战
人工智能·华为·harmonyos·鸿蒙
OH_TPC4 小时前
【鸿蒙优选三方库】@ohos/lottie:让 After Effects 动画在 HarmonyOS 上稳定播放
华为·harmonyos·鸿蒙
李蚊子5 小时前
鸿蒙应用开发实践与上架复盘
harmonyos
2501_919749036 小时前
华为鸿蒙训练口才APP—小羊口才
华为·harmonyos·鸿蒙
大锅盖16 小时前
HarmonyOS 摄像头焦点管理的完整工程实现
华为·harmonyos
woshihuanglaoshi8 小时前
鸿蒙老旧项目升级改造高级:API废弃迁移/ArkTS语法升级/架构重构策略/增量迁移/风险管控方案
学习·华为·重构·架构·harmonyos·鸿蒙
星空真迷人1 天前
嵌入式鸿蒙并非精简版,核心究竟是什么?
stm32·单片机·嵌入式硬件·物联网·华为·harmonyos·iot
OH_TPC1 天前
【鸿蒙优选三方库】@ohos/ijkplayer:在鸿蒙上像 B 站一样流畅播视频
华为·音视频·harmonyos·鸿蒙
2501_919749031 天前
华为鸿蒙经期记录APP—小羊月经
华为·harmonyos·鸿蒙