HarmonyOS Tabs组件宽度问题

文档显示Tabs组件的指示器和视图的宽度一样,app首页的会有很多栏目,在栏目的右边会有一个半透明的遮盖条和一个按钮,在这里有办法分开写吗?或者有其他的组件可以替代,这个会影响操作,最后的一两个栏目可能会点不到。

HarmonyOS
2024-12-20 16:07:28
943浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
aquaa

参考以下demo:

@Entry
@Component
struct tabTest {
  @State tabArray: Array<number> = [0, 1, 2, 3,4]
  @State focusIndex: number = 0
  @State pre: number = 0
  @State index: number = 0
  private controller: TabsController = new TabsController()
  @State test: boolean = false
  @State animationDuration: number = 300
  @State indicatorLeftMargin: number = 0
  @State indicatorWidth: number = 0
  private tabsWidth: number = 0
  private tabWidth: number = 0;
  private scrollerForScroll: Scroller = new Scroller()

  // 单独的页签
  @Builder
  Tab(tabName: string, tabItem: number, tabIndex: number) {
    Row({ space: 20 }) {
      Text(tabName)
        .fontSize(18)
        .fontColor(tabIndex === this.focusIndex ? Color.Blue :Color.Black)
        .id(tabIndex.toString())
        .onAreaChange((oldValue: Area,newValue: Area) => {
          if (this.focusIndex === tabIndex && (this.indicatorLeftMargin === 0 || this.indicatorWidth === 0)){
            if (newValue.position.x != undefined) {
              let positionX = Number.parseFloat(newValue.position.x.toString())
              this.indicatorLeftMargin = Number.isNaN(positionX) ? 0 : positionX
            }
            let width = Number.parseFloat(newValue.width.toString())
            this.tabWidth = Number.isNaN(width) ? 0 : width
            this.indicatorWidth = this.tabWidth
          }
        })
    }
    .justifyContent(FlexAlign.Center)
    .constraintSize({ minWidth: 35 })
    .width(100)
    .height(30)
    .onClick(() => {
      this.controller.changeIndex(tabIndex)
      this.focusIndex = tabIndex
    })
    .backgroundColor("#ffb7b7b7")
  }

  @Builder
  textTest(textName:string){
    Row({ space: 20 }) {
      Text(textName).fontSize(18)
    }
    .justifyContent(FlexAlign.Center)
    .constraintSize({ minWidth: 35 })
    .height(30)
    .backgroundColor("#ffb7b7b7")
  }

  build() {
    Column() {
      Stack({ alignContent: Alignment.TopStart }) {
        Column() {
          // 页签
          Row({ space: 8 }) {
            List({ space: 20, initialIndex: 0, scroller: this.scrollerForScroll }) {
              ForEach(this.tabArray, (item: number, index: number) => {
                ListItem() {
                  this.Tab("页签 " + item, item, index)
                }
              }, (item: string) => item)
            }
            .listDirection(Axis.Horizontal)
            .height(30)
            .width('80%')
            .friction(0.6)
            .alignListItem(ListItemAlign.Start)
            .scrollBar(BarState.Off)
            .width('80%')
            .backgroundColor("#ffb7b7b7")
            .onScroll((xOffset: number, yOffset: number) => {
              this.indicatorLeftMargin -= xOffset
            })
            this.textTest('更多')

          }
          .alignItems(VerticalAlign.Bottom)
          .width('100%')
          .backgroundColor("#ffb7b7b7")

        }
        .alignItems(HorizontalAlign.Start)
        .width('100%')

        Column()
          .height(2)
          .width(this.indicatorWidth)
          .margin({ left: this.indicatorLeftMargin, top:30})
          .backgroundColor(Color.Blue)
        Column()
          .height(10)
          .width("20%")
          .margin({ left: '80%', top:28})
          .backgroundColor("#ffb7b7b7")
      }
      .height(40)
      .width('100%')
      .backgroundColor("#ffb7b7b7")
      //tabs
      Tabs({ barPosition: BarPosition.Start, controller: this.controller }) {
        ForEach(this.tabArray, (item: number, index: number) => {
          TabContent() {
            Text('我是页面 ' + item + " 的内容")
              .height(300)
              .width('100%')
              .fontSize(30)
          }
          .backgroundColor(Color.White)
        }, (item: string) => item)
      }
      .onAreaChange((oldValue: Area,newValue: Area)=> {
        let width = Number.parseFloat(newValue.width.toString())
        this.tabsWidth = Number.isNaN(width) ? 0 : width
      })
      .width('100%')
      .barHeight(0)
      .animationDuration(100)
      .onChange((index: number) => {
        console.log('foo change')
        this.focusIndex = index
        this.scrollerForScroll.scrollToIndex(index-1,true)
      })
      .onAnimationStart((index: number, targetIndex: number, event: TabsAnimationEvent) => {
        // 切换动画开始时触发该回调。下划线跟着页面一起滑动
        this.focusIndex = targetIndex
        let targetIndexInfo = this.getTextInfo(targetIndex)
        this.startAnimateTo(this.animationDuration, targetIndexInfo.left, targetIndexInfo.width)
      })
      .onAnimationEnd((index: number,event: TabsAnimationEvent) => {
        // 切换动画结束时触发该回调。下划线动画停止。
        let currentIndicatorInfo = this.getCurrentIndicatorInfo(index,event)
        this.startAnimateTo(0,currentIndicatorInfo.left,currentIndicatorInfo.width)
      })
      .onGestureSwipe((index: number,event: TabsAnimationEvent) => {
        // 在页面跟手滑动过程中,逐帧触发该回调。
        let currentIndicatorInfo = this.getCurrentIndicatorInfo(index,event)
        this.focusIndex = currentIndicatorInfo.index
        this.indicatorLeftMargin = currentIndicatorInfo.left
        this.tabWidth = currentIndicatorInfo.width
        this.indicatorWidth = currentIndicatorInfo.width
      })
    }
    .height('100%')
  }

  private getTextInfo(index: number): Record<string, number> {
    let strJson = getInspectorByKey(index.toString())
    try {
      let obj: Record<string, string> = JSON.parse(strJson)
      let rectInfo: number[][] = JSON.parse('[' + obj.$rect + ']')
      return { 'left': px2vp(rectInfo[0][0]), 'width': px2vp(rectInfo[1][0] - rectInfo[0][0]) }
    } catch (error) {
      return { 'left': 0, 'width': 0 }
    }
  }
  private getCurrentIndicatorInfo(index: number, event: TabsAnimationEvent): Record<string, number> {
    let nextIndex = index
    if (index > 0 && event.currentOffset > 0) {
      nextIndex--
    } else if (index < 4 && event.currentOffset < 0) {
      nextIndex++
    }
    let indexInfo = this.getTextInfo(index)
    let nextIndexInfo = this.getTextInfo(nextIndex)
    let swipeRatio = Math.abs(event.currentOffset / this.tabsWidth)
    let currentIndex = swipeRatio > 0.5 ? nextIndex : index  // 页面滑动超过一半,tabBar切换到下一页。
    let currentLeft = indexInfo.left + (nextIndexInfo.left - indexInfo.left) * swipeRatio
    let currentWidth = indexInfo.width + (nextIndexInfo.width - indexInfo.width) * swipeRatio
    return { 'index': currentIndex, 'left': currentLeft, 'width': currentWidth }
  }

  private startAnimateTo(duration: number, leftMargin: number, width: number) {
    animateTo({
      duration: duration, // 动画时长
      curve: Curve.Linear, // 动画曲线
      iterations: 1, // 播放次数
      playMode: PlayMode.Normal, // 动画模式
      onFinish: () => {
        console.info('play end')
      }
    }, () => {
      this.indicatorLeftMargin = leftMargin
      this.tabWidth = width
      this.indicatorWidth = width
    })
  }
}
  • 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.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
  • 170.
  • 171.
  • 172.
  • 173.
  • 174.
  • 175.
  • 176.
  • 177.
  • 178.
  • 179.
  • 180.
  • 181.
  • 182.
  • 183.
  • 184.
  • 185.
  • 186.
  • 187.
  • 188.
  • 189.
  • 190.
  • 191.
  • 192.
  • 193.
  • 194.
  • 195.
  • 196.
分享
微博
QQ
微信
回复
2024-12-20 17:47:10
相关问题
HarmonyOS Tabs组件的tabBar宽度问题
1402浏览 • 1回复 待解决
HarmonyOS Tabs组件嵌套Tabs组件问题
1777浏览 • 1回复 待解决
HarmonyOS Flex组件宽度问题
1042浏览 • 1回复 待解决
HarmonyOS Tabs组件组件问题
1400浏览 • 1回复 待解决
HarmonyOS 关于Tabs组件的TabContent问题
740浏览 • 1回复 待解决
HarmonyOS Tabs组件bar背景设置问题
1377浏览 • 1回复 待解决
HarmonyOS 关于动态设置组件宽度问题
1178浏览 • 1回复 待解决
Tabs组件TabContent滑到边缘问题
1065浏览 • 0回复 待解决
Tabs组件懒加载的问题
2907浏览 • 1回复 待解决
HarmonyOS RN Bottom-tabs组件返回问题
814浏览 • 1回复 待解决
HarmonyOS Popup宽度问题
633浏览 • 1回复 待解决
HarmonyOS Tabs组件的Tab栏滚动问题
1582浏览 • 1回复 待解决
HarmonyOS tabs切换问题
457浏览 • 1回复 待解决
HarmonyOS Tabs位置问题
571浏览 • 1回复 待解决
HarmonyOS tabs对齐问题
505浏览 • 1回复 待解决
HarmonyOS Tabs组件Tabs如何左对齐?
1245浏览 • 1回复 待解决
Tabs组件自定义导航栏UI问题
1515浏览 • 1回复 待解决
HarmonyOS 折叠屏webview宽度问题
1149浏览 • 1回复 待解决
HarmonyOS Tabs组件切换
1090浏览 • 1回复 待解决
HarmonyOS组件超出父组件宽度
874浏览 • 1回复 待解决