鸿蒙基本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、运行结果:

相关推荐
nashane9 小时前
HarmonyOS 6学习:外接键盘CapsLock与长截图功能的实战调试与完整解决方案
学习·华为·计算机外设·harmonyos
aqi0017 小时前
一文理清 HarmonyOS 6.0.2 涵盖的十个升级点
android·华为·harmonyos·鸿蒙·harmony
环信即时通讯云19 小时前
环信Flutter UIKit适配鸿蒙实战指南
flutter·华为·harmonyos
Swift社区19 小时前
鸿蒙 PC 应用启动优化全解析
华为·harmonyos
richard_yuu21 小时前
鸿蒙本地数据存储实战|Preferences 封装、数据隔离与隐私合规存储方案
android·华为·harmonyos
Lynnb21 小时前
Mac电脑烧录 RK3588 鸿蒙开发板固件教程
华为·harmonyos·鸿蒙系统
我头上有犄角ovo1 天前
HarmonyOS 测肤拍照页实战:Metadata 实时取景 + Core Vision 拍后校验,从 0.001 的 widthRatio 踩坑到可上线
前端·harmonyos
码农小北1 天前
MAC 配置鸿蒙(HarmonyOS) SDK 环境变量完整指南
macos·华为·harmonyos
weixin_386468961 天前
openharmony 6.0编译rk3568过程记录
c语言·c++·git·python·vim·harmonyos·openharmony