HarmonyOS Tabs无法改变tab的字体颜色,同时无法靠前布局?

Tabs控件在使用时无法改变tab的字体颜色,同时在设置scorllable模式的时候无法靠前开始布局

Tabs({
  index: this.selectedIndex,
  barPosition: BarPosition.Start,
  controller: this.controller,
}) {
  ForEach(this.segmentData, (item: HPCustomSegmentData, tabIndex: number) => {
    TabContent() {
      if (this.contentBuilders[tabIndex].wrappedbuilder) {
        this.contentBuilders[tabIndex].wrappedbuilder?.builder(this.contentBuilders[tabIndex].param)
      }
    }
    .tabBar(SubTabBarStyle.of(this.segmentData[tabIndex].title)
      .indicator(
        {
          color: $r('app.color.primary_button'), //下划线颜色
          height: 2, //下划线高度
          width: 30, //下划线宽度
          marginTop: 16 //下划线与文字间距
        })
      .selectedMode(SelectedMode.INDICATOR)
      .labelStyle({})
    )
  })
}
.vertical(false)
.scrollable(true)
.backgroundColor($r('app.color.bg'))
.barWidth('100%')
.barMode(BarMode.Scrollable)
.width(CommonConstants.FULL_PERCENT)
.layoutWeight(1)
.onChange((index: number) => {
  this.selectedIndex = index;
})
阅读 475
1 个回答

1.按照提供的demo,切换active tabBar是没问题的:

@Entry
@Component
struct toastExample {
  @State selectedIndex:number = 0
  @State segmentData: number[] = [0, 1, 2, 3,]
  private controller: TabsController = new TabsController()
  @Builder tabBuilder(title: string,targetIndex: number) {
    Column(){
      Text(title).fontColor(this.selectedIndex === targetIndex ? '#1698CE' : '#6B6B6B')
    }.width('100%')
    .height(50)
    .justifyContent(FlexAlign.Center)
  }
  build() {
    Column() {
      Tabs({
        index: this.selectedIndex,
        barPosition: BarPosition.Start,
        controller: this.controller,
      }) {
        ForEach(this.segmentData, (item: number, tabIndex: number) => {
          TabContent() {
            Column(){
              Text('content' +item)
            }
            .width('100%').height('100%').backgroundColor(Color.Pink).borderRadius('12vp')
          }.tabBar(SubTabBarStyle.of('标题')
            .indicator({
              color: Color.Blue, //下划线颜色
              height: 2, //下划线高度
              width: 30, //下划线宽度
              borderRadius: 5, //下划线圆角半径
              marginTop: 10 //下划线与文字间距
            })
            .selectedMode(SelectedMode.INDICATOR)
            .labelStyle({})
          )
        })
      }
      .vertical(false)
      .scrollable(true)
      .backgroundColor(Color.Pink)
      .barWidth('100%')
      .barMode(BarMode.Scrollable)
      .width('100%')
      .layoutWeight(1)
      .onChange((index: number) => {
        this.selectedIndex = index;
      })

    }.height('100%').width('100%').justifyContent(FlexAlign.Center)
  }
}

2.系统提供的Tabs暂不支持居左显示,可以通过Scroll和Row组件用来实现一个页签,自定义tabbar,在onclick事件中通过修改索引值和Tabs组件的索引联动,实现切换效果,同时将Tabs的barHeight置为0

@Entry
@Component
struct TabsExample {
  @State tabArray: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
  @State focusIndex: number = 0;
  private controller: TabsController = new TabsController();

  build() {
    Column() {
      // 使用自定义页签组件
      Scroll() {
        Row() {
          ForEach(this.tabArray, (item: number, index: number) => {
            Row({ space: 20 }) {
              Text('页签' + item)
                .fontColor(index === this.focusIndex ? Color.Red : Color.Black)
                .fontWeight(index === this.focusIndex ? FontWeight.Bold : FontWeight.Normal)
            }
            .padding({ left: 10, right: 10 })
            .onClick(() => {
              this.controller.changeIndex(index);
              this.focusIndex = index;
            })
          })
        }
      }
      .align(Alignment.Start)
      .scrollable(ScrollDirection.Horizontal)
      .scrollBar(BarState.Off)
      .width('100%')

      //tabs组件把tabbar隐藏
      Tabs({ barPosition: BarPosition.Start, controller: this.controller }) {
        ForEach(this.tabArray, (item: number, index: number) => {
          TabContent() {
            Text('我是页面 ' + item + " 的内容")
              .fontSize(30)
          }
        })
      }.barHeight(0)
    }
    .height('100%')
    .width('100%')
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进