HarmonyOS 想要tabs的高度随着子组件的高度而变化

现在tabs的高度是固定的,想要scroll->tabs->tabscontent->list这种结构能让scroll滑动起来,因为tabs的高度是固定的,所以这个滑动处理不了。

HarmonyOS
2025-01-09 16:32:25
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
put_get

首先需要确保子组件的高度是动态变化的,将Tabs组件的height属性设置为auto,即可实现tabs的高度随着子组件的高度而变化。

参考示例如下:

@Entry
@Component
export struct EPage {
  @State arr: number[] = []
  @State isRefreshing: boolean = false
  @State listFlag: boolean = true
  private scroller: Scroller = new Scroller();

  @Styles
  listCard() {
    .backgroundColor(Color.White)
    .height(72)
    .width("100%")
    .borderRadius(12)
  }

  build() {

    Column() {
      Search()
      Scroll() {
        Column() {
          Text("Scroll Area")
            .width("100%")
            .height(60)
            .backgroundColor('#0080DC')
            .textAlign(TextAlign.Center)
          Refresh({ refreshing: $$this.isRefreshing }) {
            Scroll() {
              Column() {
                Image($r('app.media.startIcon'))
                  .width("100%")
                  .height(200)
                Tabs({ barPosition: BarPosition.Start }) {
                  TabContent() {
                    List({ space: 10, scroller: this.scroller }) {
                      ForEach(this.arr, (item: number) => {
                        ListItem() {
                          Text("item" + item)
                            .fontSize(16)
                        }.listCard()
                      }, (item: string) => item)
                      ListItem() {
                        CustomLayout()
                      }
                      .visibility(this.listFlag ? Visibility.Visible : Visibility.None)
                    }.width("100%")
                    .onReachEnd(() => {
                      setTimeout(() => {
                        this.listFlag = false
                      }, 2000)
                    })
                    .edgeEffect(EdgeEffect.Spring)
                    .nestedScroll({
                      scrollForward: NestedScrollMode.PARENT_FIRST,
                      scrollBackward: NestedScrollMode.SELF_FIRST
                    })
                  }.tabBar("Tab1")

                  TabContent() {
                  }.tabBar("Tab2")
                }
                .vertical(false)
                .height("100%")
              }
            }
            .nestedScroll({
              scrollForward: NestedScrollMode.SELF_FIRST,
              scrollBackward: NestedScrollMode.PARENT_FIRST
            })

            .edgeEffect(EdgeEffect.Spring)
            .friction(0.6)
            .backgroundColor('#DCDCDC')
            .scrollBar(BarState.Off)
            .width('100%')
            .height('100%')
          }
          .onRefreshing(() => {
            setTimeout(() => {
              this.isRefreshing = false
            }, 2000)
            console.log('onRefreshing test')
          })
        }.width("100%")
      }
      .edgeEffect(EdgeEffect.Spring)
      .friction(0.6)
      .backgroundColor('#DCDCDC')
      .scrollBar(BarState.Off)
      .width('100%')
      .height('100%')
    }
  }

  aboutToAppear() {
    for (let i = 0; i < 30; i++) {
      this.arr.push(i)
    }
  }
}

@Component
export default struct CustomLayout {
  build() {
    Row() {
      Image($r('app.media.ic_pull_up_load'))
        .width(18)
        .height(18)

      Text('加载中...')
        .margin({
          left: 7,
          bottom: 1
        })
        .fontSize(17)
        .textAlign(TextAlign.Center)
    }
    .clip(true)
    .width('100%')
    .justifyContent(FlexAlign.Center)
    .height(70)
  }
}
  • 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.
分享
微博
QQ
微信
回复
2025-01-09 19:03:32


相关问题
HarmonyOS Tabs默认高度问题
816浏览 • 1回复 待解决
HarmonyOS 控件高度随滚动变化
771浏览 • 1回复 待解决
HarmonyOS Tabs TabBuild高度被系统限定
649浏览 • 1回复 待解决
鸿蒙如何监听键盘高度变化
209浏览 • 1回复 待解决
HarmonyOS 页面高度发生变化
831浏览 • 1回复 待解决