HarmonyOS Tabs里的barMode中的ScrollableBarModeOptions?

Tabs里的barMode中的ScrollableBarModeOptions中的margin怎么设置一边设置距离:比方left或者right,而不是同时设置

需要设置一边有距离

阅读 410
1 个回答

当前的margin只支持Dimension属性:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-container-tabs-V5\#scrollablebarmodeoptions10对象说明

可以设置.barMode(BarMode.Scrollable),自定义每个tabBar的宽度:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-container-tabs-V5\#barmode10

// xxx.ets
@Entry
@Component
struct RefreshExample {
  @State isRefreshing: boolean = false
  @State fontColor: string = '#182431'
  @State selectedFontColor: string = '#007DFF'
  @State currentIndex: number = 0
  private controller: TabsController = new TabsController()
  @State arr:string[] = ['green','blue','yellow','pink']
  @State isShow:boolean = true
  @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 })
        .backgroundColor(Color.Red)
      Divider()
        .strokeWidth(2)
        .color('#007DFF')
        .opacity(this.currentIndex === index ? 1 : 0)
    }.width(index==0?200:100)
  }

  @Builder
  tabMain(){
    Row(){
      Tabs({ barPosition: BarPosition.Start, index: this.currentIndex, controller: this.controller }) {
        ForEach(this.arr,(item:string,index)=>{
          TabContent() {
            tabContentInfo({text:item})
          }.tabBar(this.tabBuilder(index, (index+1).toString()))
        })
      }
      .vertical(false)
      .barMode(BarMode.Scrollable)
      .barWidth(360)
      .barHeight(56)
      .animationDuration(400)
      .onChange((index: number) => {
        this.currentIndex = index
      })
      .backgroundColor('#F1F3F5')
    }
    .width('100%')
    .height('100%')
  }

  build() {
    Column() {
      Refresh({ refreshing: $$this.isRefreshing}) {
        if(this.isShow){
          this.tabMain()
        }else{
          this.tabMain()
        }
      }
      .onStateChange((refreshStatus: RefreshStatus) => {
        // console.info('Refresh onStatueChange state is ' + refreshStatus)
      })
      .onOffsetChange((value: number) => {
        // console.info('Refresh onOffsetChange offset:' + value)
      })
      .onRefreshing(() => {
        setTimeout(() => {
          this.isRefreshing = false
          this.isShow =!this.isShow
        }, 2000)
        console.log('onRefreshing test')
      })
      .backgroundColor(0x89CFF0)
      .refreshOffset(64)
      .pullToRefresh(true)
    }
  }
}

@Component
export struct tabContentInfo{

  @Prop text:string
  aboutToAppear(): void {
    console.info("进入aboutToAppear:",this.text)
  }

  build() {
    Column(){
      Text(this.text)
      Button('改变')
        .onClick(()=>{
          this.text += '-'
        })
    }
    .width("100%")
    .height("100%")
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进