HarmonyOS 网格布局是否可以自适应高度?根据数据的数量 自适应高度

网格布局是否可以自适应高度?根据数据的数量 自适应高度

HarmonyOS
2024-12-20 17:13:27
5269浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
fox280

通过计算展示的行数rowCount,根据rowCount得出GridHeight、GridRowsTemplate。参考demo:

@Entry
@Component
struct ABPage {
  @State message: string = 'Hello World';
  private controller: TextInputController = new TextInputController()
  @State simpleList: Array<string> = ['one', 'two', 'three', 'one', 'two', 'three','one', 'two', 'three', 'one', 'two',];
  getCategoryRowCount() {
    return Math.ceil(this.simpleList.length / 3);
  }
  getCategoryRowTmpl() {
    const count = this.getCategoryRowCount();
    const arr: string[] = new Array(count || 1).fill('1fr');
    console.log('tmpl ', arr.join(' '))
    return arr.join(' ');
  }
  getCategoryViewHeight() {
    return `${50 * this.getCategoryRowCount()}vp`;
  }
  build() {
    Column() {
      Column() {
        Grid() {
          ForEach(this.simpleList, (item: string) => {
            this.gridItemBuilder(item)
          })
        }
        .height(this.getCategoryViewHeight())
        .rowsTemplate(this.getCategoryRowTmpl())
        .columnsTemplate('1fr 1fr 1fr ')
      }
      // .height("15%")
      .backgroundColor('#ff02194c')
      .justifyContent(FlexAlign.SpaceEvenly)
      Column() {
        List() {
          ForEach(this.simpleList, (item: string) => {
            this.gridItemBuilder(item)
          })
        }
      }
      .backgroundColor('#ffcff10c')
      .height("100%")

    }
    .width('94%')
    .height('80%')
    .borderRadius(10)
    .backgroundColor('#ff8f1616')
    .justifyContent(FlexAlign.Start)
    .margin({ top: '10%', left: '3%', right: '3%',bottom:'10%' })
  }

  @Builder
  gridItemBuilder(content: string) {
    GridItem() {
      Column() {
        Text(content)
          .fontSize(15)
      }
      .justifyContent(FlexAlign.Center)
      .borderRadius(10)
      .backgroundColor('#ffeabe4f')
      .height('25vp')
      .width('100%')
      .margin({ top: '5vp' })
      .onClick(() => {

      })
    }
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
分享
微博
QQ
微信
回复
2024-12-20 19:13:54
相关问题
HarmonyOS List高度根据内容自适应
1026浏览 • 1回复 待解决
HarmonyOS Grid高度根据内容自适应
843浏览 • 1回复 待解决
HarmonyOS List是否可以设置自适应高度
640浏览 • 1回复 待解决
List水平布局如何根据内容自适应高度
1533浏览 • 1回复 待解决
HarmonyOS 高度自适应
1048浏览 • 1回复 待解决
HarmonyOS RichText自适应高度
753浏览 • 1回复 待解决
HarmonyOS 高度自适应问题
1041浏览 • 1回复 待解决
HarmonyOS web高度自适应内容高度
1178浏览 • 1回复 待解决
HarmonyOS RelativeContainer无法自适应高度
1268浏览 • 1回复 待解决
HarmonyOS Web高度自适应问题
1601浏览 • 1回复 待解决
HarmonyOS webview高度不能自适应
664浏览 • 1回复 待解决
HarmonyOS RichText能否自适应高度
650浏览 • 1回复 待解决
HarmonyOS RelativeContainer自适应高度相关
766浏览 • 1回复 待解决
HarmonyOS GridItem自适应高度问题
1083浏览 • 1回复 待解决
HarmonyOS 如何实现自适应web高度
634浏览 • 1回复 待解决
HarmonyOS 自适应父组件高度问题
1868浏览 • 1回复 待解决
HarmonyOS web组件怎么自适应高度
754浏览 • 1回复 待解决
HarmonyOS Grid组件能否高度自适应
769浏览 • 1回复 待解决
HarmonyOS web组件自适应高度问题
1802浏览 • 1回复 待解决
HarmonyOS Grid自适应高度和拖拽问题
1309浏览 • 1回复 待解决
Web组件如何实现高度自适应
1739浏览 • 1回复 待解决