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

相关推荐
小雨青年2 小时前
鸿蒙 HarmonyOS 6 | AI Kit 集成 Core Speech Kit 语音服务
人工智能·华为·harmonyos
一起养小猫3 小时前
Flutter for OpenHarmony 实战 表单处理与验证完整指南
android·开发语言·前端·javascript·flutter·harmonyos
摘星编程4 小时前
React Native鸿蒙版:自定义useMask输入掩码
react native·react.js·harmonyos
mocoding4 小时前
使用Flutter设置UI三方库card_settings_ui重构鸿蒙版天气预报我的页面
flutter·ui·harmonyos
摘星编程4 小时前
OpenHarmony + RN:自定义useValidator表单验证
react native·react.js·harmonyos
摘星编程6 小时前
OpenHarmony环境下React Native:自定义useFieldArray字段数组
react native·react.js·harmonyos
2601_949593656 小时前
基础入门 React Native 鸿蒙跨平台开发:FlatList 性能优化
react native·性能优化·harmonyos
熊猫钓鱼>_>7 小时前
【开源鸿蒙跨平台开发先锋训练营】Day 15: 赋予应用生命力——React Native原生动效体系构建
react native·华为·开源·harmonyos·arkts·鸿蒙
大雷神7 小时前
HarmonyOS智慧农业管理应用开发教程--高高种地--第16篇:HarmonyOS AI能力概述与集成
人工智能·华为·harmonyos