HarmonyOS Scroll中嵌套Grid组件的时候,Grid必须要设置height高度吗

Scroll中嵌套Grid组件的时候,Grid必须要设置height高度吗

HarmonyOS
2024-12-20 15:29:26
1046浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
shlp

Grid的宽高没有设置时,默认适应父组件尺寸。 将 代码中Grid的父组件(Column)的宽高设置下比如: .width(“100%”) .height(“100%”)我这边是可以正常显示出来的。

参考demo:

@Entry
@Component
export struct AllChannelsPage{

  @State tabs: Array<String> = new Array();

  @State dragChannel: string = 'drag';

  private deviceWidth: number = (AppStorage.get('deviceWidth') as number)
  @State itemWidth: number = 80;
  @State itemHeight: number = 80;
  @State mineRowCount: number = 1;
  @State mineGridHeight: number = 100;
  @State mainTitieDes: string = '点击进入频道';

  aboutToAppear(): void {
    for(let i = 0; i < 30; i++ ){
      this.tabs.push('频道' + i);
    }
    this.itemWidth = (this.deviceWidth - 32 - 30)/4;
    this.itemHeight = this.itemWidth / 2.1;
    this.mineRowCount = Math.ceil(this.tabs.length / 4);
    this.mineGridHeight = this.mineRowCount * this.itemHeight + (this.mineRowCount - 1) * 10
  }

  @Builder pixelMapBuilder() { //拖拽过程样式
    Column() {
      Text(this.dragChannel)
        .fontSize('15fp')
          // .fontColor($r('app.color.color202022'))
        .textAlign(TextAlign.Center)
        .width(this.itemWidth)
        .height(this.itemHeight)
          // .backgroundColor($r('app.color.colorF9F9F9'))
        .borderRadius(4)
    }
  }

  changeIndex(index1: number, index2: number) { //交换数组位置
    let temp = this.tabs[index1];
    this.tabs[index1] = this.tabs[index2];
    this.tabs[index2] = temp;
  }

  build() {
    NavDestination(){
      Column(){
        Scroll(){
          Column(){
            Row(){
              Text().width(4).height(16)
                // .backgroundColor($r('app.color.colorF21333'))
                .borderRadius(2)
              Blank().width(6)
              Text('我的频道').fontSize('16fp')
              // .fontColor($r('app.color.color202022'))
              Blank().width(8)
              Text(this.mainTitieDes).fontSize('12fp')
              // .fontColor($r('app.color.colorB1B1BB'))
              Blank().layoutWeight(1)
              Text('重置').fontSize('15fp')
              // .fontColor($r('app.color.color909099'))
              Text().width(1).height(10)
                // .backgroundColor($r('app.color.color909099'))
                .margin({left: 4, right: 4})
              Text('编辑').fontSize('15fp')
              // .fontColor($r('app.color.colorF21333'))
            }.width('100%')
            .margin({top: 5, bottom: 15})
            .padding({ left: 16, right: 16 })

            Grid() {
              ForEach(this.tabs, (channel: string) => {
                GridItem() {
                  Text(channel)
                    .fontSize((channel?? '').length > 5 ? '11fp': '15fp')
                      // .fontColor($r('app.color.color202022'))
                    .textAlign(TextAlign.Center)
                      // .width(this.itemWidth)
                      // .height(this.itemHeight)
                    .width('100%')
                    .height(80)
                      // .backgroundColor($r('app.color.colorF9F9F9'))
                    .borderRadius(4)
                  // .onTouch((event: TouchEvent) => {
                  //   if (event.type === TouchType.Up) {
                  //     this.dragChannel = channel.channelName ?? '';
                  //   }
                  // })
                }
              })
            }
            .columnsTemplate('1fr 1fr 1fr 1fr')
            .columnsGap(10)
            .rowsGap(10)
            .onScrollIndex((first: number) => {
              console.info(first.toString());
            })
            // .width('100%')
            // .height('80%')
            .padding({ left: 16, right: 16 })
            .supportAnimation(true)
            .editMode(true) //设置Grid是否进入编辑模式,进入编辑模式可以拖拽Grid组件内部GridItem
            .onItemDragStart((event: ItemDragInfo, itemIndex: number) => { //第一次拖拽此事件绑定的组件时,触发回调。
              return this.pixelMapBuilder(); //设置拖拽过程中显示的图片。
            })
            .onItemDrop((event: ItemDragInfo, itemIndex: number, insertIndex: number, isSuccess: boolean) => { //绑定此事件的组件可作为拖拽释放目标,当在本组件范围内停止拖拽行为时,触发回调。
              console.info('tag' + itemIndex + '', insertIndex + '') //itemIndex拖拽起始位置,insertIndex拖拽插入位置
              this.changeIndex(itemIndex, insertIndex)
            })
            .nestedScroll({
              scrollForward: NestedScrollMode.PARENT_FIRST,
              scrollBackward: NestedScrollMode.SELF_FIRST
            })

            Text().width('100%').height(500)
          }
          .width("100%")
          .height("100%")
        }
        .width('100%')
        .layoutWeight(1)

      }

    }.hideTitleBar(true)
  }

}
  • 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.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
分享
微博
QQ
微信
回复
2024-12-20 18:17:57


相关问题
HarmonyOS List组件不能嵌套Grid组件
685浏览 • 1回复 待解决
HarmonyOS Grid-GridItem组件高度咨询
602浏览 • 1回复 待解决
HarmonyOS Grid组件能否高度自适应
777浏览 • 1回复 待解决
HarmonyOS Tabs嵌套Grid问题
744浏览 • 1回复 待解决
Grid嵌套滚动问题有知道
3612浏览 • 1回复 待解决
Grid组件如何实现高度自适应
4220浏览 • 1回复 待解决
HarmonyOS Grid组件能否自适应内容高度?
677浏览 • 1回复 待解决
HarmonyOS List嵌套List和List嵌套Grid问题
902浏览 • 1回复 待解决
HarmonyOS Grid高度根据内容自适应
857浏览 • 1回复 待解决
HarmonyOS Grid自适应高度和拖拽问题
1326浏览 • 1回复 待解决
Grid组件如何实现拖拽排序?
303浏览 • 0回复 待解决
HarmonyOS Grid高度无法等分(横3竖2)
1060浏览 • 1回复 待解决