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

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

HarmonyOS
2024-12-20 17:13:27
浏览
收藏 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(() => {

      })
    }
  }
}
分享
微博
QQ
微信
回复
2024-12-20 19:13:54
相关问题
HarmonyOS List高度根据内容自适应
469浏览 • 1回复 待解决
HarmonyOS Grid高度根据内容自适应
370浏览 • 1回复 待解决
List水平布局如何根据内容自适应高度
1030浏览 • 1回复 待解决
HarmonyOS List是否可以设置自适应高度
167浏览 • 1回复 待解决
HarmonyOS RichText自适应高度
315浏览 • 1回复 待解决
HarmonyOS 高度自适应
621浏览 • 1回复 待解决
HarmonyOS web高度自适应内容高度
548浏览 • 1回复 待解决
HarmonyOS RelativeContainer无法自适应高度
773浏览 • 1回复 待解决
HarmonyOS webview高度不能自适应
194浏览 • 1回复 待解决
HarmonyOS Web高度自适应问题
1109浏览 • 1回复 待解决
HarmonyOS RichText能否自适应高度
266浏览 • 1回复 待解决
HarmonyOS 高度自适应问题
631浏览 • 1回复 待解决
HarmonyOS RelativeContainer自适应高度相关
373浏览 • 1回复 待解决
HarmonyOS GridItem自适应高度问题
658浏览 • 1回复 待解决
HarmonyOS 如何实现自适应web高度
178浏览 • 1回复 待解决
HarmonyOS Grid自适应高度和拖拽问题
902浏览 • 1回复 待解决
HarmonyOS 自适应父组件高度问题
1420浏览 • 1回复 待解决
HarmonyOS web组件自适应高度问题
1381浏览 • 1回复 待解决
HarmonyOS web组件怎么自适应高度
351浏览 • 1回复 待解决
HarmonyOS Grid组件能否高度自适应
306浏览 • 1回复 待解决
Web组件如何实现高度自适应
1318浏览 • 1回复 待解决