#鸿蒙通关秘籍#在HarmonyOS NEXT中如何配置TabBar并自定义每个Tab的样式?

HarmonyOS
2024-11-28 15:55:06
1139浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
编程小战士

在HarmonyOS NEXT中配置TabBar并自定义每个Tab的样式,使用一个构建方法TabBuilder来动态设置每个Tab的图标和文字颜色。通过不同的状态选择不同的图标,点击后更新currentIndex和Tab的状态:

@Builder TabBuilder(title: string, targetIndex: number, selectedImg: Resource, normalImg: Resource) {
  Column() {
    Image(this.currentIndex === targetIndex ? selectedImg : normalImg)
      .size({ width: 25, height: 25 })
    Text(title)
      .fontColor(this.currentIndex === targetIndex ? '#28bff1' : '#8a8a8a')
  }
  .width('100%')
  .height(50)
  .justifyContent(FlexAlign.Center)
  .onClick(() => {
    this.currentIndex = targetIndex
    this.controller.changeIndex(this.currentIndex)
  })
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

通过该构建器,依据当前选中状态调整图标和文字颜色,将点击事件绑在Column上以改变currentIndex,从而实现切换。

分享
微博
QQ
微信
回复
2024-11-28 16:21:57
相关问题
HarmonyOS 怎么自定义TabTabbar
683浏览 • 1回复 待解决