HarmonyOS 切换tabs的时候,该如何修改其他图片的背景图片

写了一个Tabs,然后当切换tabBar的时候,需要改正整个页面风格的背景图片,但是这样写

@Builder
tabLabel(title: string, targetIndex: number) {
  if(targetIndex==0){
    this.appResource =  $r('app.media.ic_left_arrow')
  }else if(targetIndex==1){
  }else if(targetIndex==2){
  }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

会提示不允许这种语法,所以应该怎么来实现

HarmonyOS
2024-12-23 14:54:38
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
FengTianYa

可以参考此demo

@Entry
@Component
struct TabsExample {
  @State fontColor: string = '#182431'
  @State selectedFontColor: string = '#007DFF'
  @State currentIndex: number = 0
  private controller: TabsController = new TabsController()
  @State imageRecourse:Resource = $rawfile('B.jpg')
  @Builder tabBuilder(index: number, name: string) {
    Column() {
      Text(name)
        .fontColor(this.currentIndex === index ? this.selectedFontColor : this.fontColor)
        .fontSize(16)
        .fontWeight(this.currentIndex === index ? 500 : 400)
        .lineHeight(22)
        .margin({ top: 17, bottom: 7 })
      Divider()
        .strokeWidth(2)
        .color('#007DFF')
        .opacity(this.currentIndex === index ? 1 : 0)
    }.width('100%')
  }

  build() {
    Column() {
      Tabs({ barPosition: BarPosition.Start, index: this.currentIndex, controller: this.controller }) {
        TabContent() {
          Column().width('100%').height('100%').backgroundColor('#00CB87')
        }.tabBar(this.tabBuilder(0, 'green'))

        TabContent() {
          Column().width('100%').height('100%').backgroundColor('#007DFF')
        }.tabBar(this.tabBuilder(1, 'blue'))

        TabContent() {
          Column().width('100%').height('100%').backgroundColor('#FFBF00')
        }.tabBar(this.tabBuilder(2, 'yellow'))

        TabContent() {
          Column().width('100%').height('100%').backgroundColor('#E67C92')
        }.tabBar(this.tabBuilder(3, 'pink'))
      }
      .vertical(false)
      .barMode(BarMode.Fixed)
      .barWidth(360)
      .barHeight(56)
      .animationDuration(400)
      .onChange((index: number) => {
        this.currentIndex = index
        console.log(this.currentIndex.toString())
        if(this.currentIndex == 1) {
          this.imageRecourse = $rawfile('C.jpg')
        } else if (this.currentIndex == 0) {
          this.imageRecourse =  $rawfile('B.jpg')
        }
      })
      .width(360)
      .height(296)
      .margin({ top: 52 })
      .backgroundColor('#F1F3F5')
      Image(this.imageRecourse).width('100px').height('100px')
      Button('更改').onClick(()=>{
        this.imageRecourse = $rawfile('C.jpg')
      })
    }.width('100%')
  }
}
  • 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.
分享
微博
QQ
微信
回复
2024-12-23 18:06:31
相关问题
HarmonyOS 背景图片如何填充满组件
1591浏览 • 1回复 待解决
HarmonyOS 对于图片或者背景图拉伸
810浏览 • 1回复 待解决
HarmonyOS 如何背景图居中
884浏览 • 1回复 待解决
HarmonyOS 设置冷启动背景图
1504浏览 • 1回复 待解决