HarmonyOS NEXT Tabs 是否能支持 tabbar 居左对齐?

阅读 661
avatarAI BotBETA

答案

HarmonyOS NEXT Tabs 支持 tabbar 居左对齐。开发者可以通过设置相关的属性或样式来实现这一布局效果。具体实现方式可能依赖于使用的具体框架或组件库,建议查阅相关的开发文档或示例代码。

1 个回答

当前方案为自定义tabbar实现,示例demo:

@Entry 
@Component 
struct TabsExample { 
  @State tabArray: Array<number> = [0, 1,2] 
  @State focusIndex: number = 0 
  @State pre: number = 0 
  @State index: number = 0 
  private controller: TabsController = new TabsController() 
  @State test: boolean = false 
  // 单独的页签 
  @Builder 
  Tab(tabName: string, tabItem: number, tabIndex: number) { 
    Row({ space: 20 }) { 
      Text(tabName).fontSize(18) 
        .fontColor(tabItem===this.focusIndex?Color.Red:Color.Black) 
    } 
    .justifyContent(FlexAlign.Center) 
    .constraintSize({ minWidth: 35 }) 
    .width(80) 
    .height(60) 
    .borderRadius({ topLeft: 10, topRight: 10 }) 
    .onClick(() => { 
      this.controller.changeIndex(tabIndex) 
      this.focusIndex = tabIndex 
    }) 
  } 
  build() { 
    Column() { 
      Column() { 
        // 页签 
        Row({ space: 7 }) { 
          Scroll() { 
            Row() { 
              ForEach(this.tabArray, (item: number, index: number) => { 
                this.Tab("页签 " + item, item, index) 
              }) 
            } 
            .justifyContent(FlexAlign.Start) 
          } 
          .align(Alignment.Start) 
          .scrollable(ScrollDirection.Horizontal) 
          .scrollBar(BarState.Off) 
          .width('90%') 
        } 
        .alignItems(VerticalAlign.Bottom) 
        .width('100%') 
      } 
      .alignItems(HorizontalAlign.Start) 
      .width('100%') 
      //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.Pink) 
        }) 
      } 
      .width('100%') 
      .barHeight(0) 
      .animationDuration(100) 
      .onChange((index: number) => { 
        console.log('foo change') 
        this.focusIndex = index}) 
    } 
    .height('100%') 
  } 
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进