HarmonyOS Grid组件能否自适应内容高度?

目前我girdItem 是通过接口遍历的 不确定高度 grid组件好像默认是撑满屏幕 能否设置为内容多高它多高

阅读 518
1 个回答

可以设置maxcount属性看看

@Entry
@Component
struct CalcGridHeight {
  build() {
    Column() {
      Text('一段文字')
      Grid() {
        ForEach([1, 2, 3, 4, 5, 6, 8, 9, 10, 7, 1, 1, 1, 1, 1, 1, 1], (item: number, index: number) => {
          GridItem() {
            Column() {
              Text(item.toString())
                .width('100%')
                .backgroundColor('#ccc')
              if (index === 0) {
                Text('test').height(30)
              }
            }
          }
          .width('20%')
          .padding(12)
        })
      }.maxCount(5)


      Text('一段文字')
    }.height('100%')
    .width('100%')
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进