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

相关推荐
2501_920627611 天前
Flutter 框架跨平台鸿蒙开发 - 古文学习应用
学习·flutter·harmonyos
芙莉莲教你写代码1 天前
Flutter 框架跨平台鸿蒙开发 - 魔术教学
flutter·华为·harmonyos
纯爱掌门人1 天前
鸿蒙文件预览开发实践:从打开文件到加速感知
华为·harmonyos
云和数据.ChenGuang1 天前
当智能体遇上原生鸿蒙:开启下一代操作系统的“智慧觉醒”
华为·harmonyos
左手厨刀右手茼蒿1 天前
Flutter 三方库 bs58 的鸿蒙化适配指南 - 在鸿蒙系统上构建极致、高效的 Base58 数字货币与区块链数据编解码引擎
flutter·harmonyos·鸿蒙·openharmony
加农炮手Jinx1 天前
Flutter 组件 substrate_bip39 的适配 鸿蒙Harmony 实战 - 驾驭区块链级助记词原语、实现鸿蒙端金融级 BIP39 安全私钥推导方案
flutter·harmonyos·鸿蒙·openharmony·substrate_bip39
左手厨刀右手茼蒿1 天前
Flutter 组件 substrate_bip39 的适配 鸿蒙Harmony 实战 - 驾驭区块链级 BIP39 安全底座、实现鸿蒙端私钥派生与国密级密钥保护方案
flutter·harmonyos·鸿蒙·openharmony·substrate_bip39
加农炮手Jinx1 天前
Flutter 三方库 fast_base58 的鸿蒙化进阶指南 - 挑战编解码吞吐量极限、助力鸿蒙端大规模区块链与分布式存储数据处理
flutter·harmonyos·鸿蒙·openharmony·fast_base58
里欧跑得慢1 天前
Flutter 三方库 ethereum 鸿蒙分布式区块链数字资产上链钱包适配突破:接通 JSON-RPC 加密管线深入打通智能合约闭环实现高价值数字加密交互-适配鸿蒙 HarmonyOS ohos
分布式·flutter·harmonyos
2501_920627611 天前
Flutter 框架跨平台鸿蒙开发 - 压力管理助手应用
flutter·华为·harmonyos