HarmonyOS使用tabs组件怎么让多个tab标题从左到右显示,而不是居中显示

HarmonyOS使用tabs组件怎么让多个tab标题从左到右显示,而不是居中显示

HarmonyOS
2024-08-04 17:45:34
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
FengTianYa

示例代码如下:

@Entry 
@Component 
struct Index { 
  @State tabArray: Array<number> = [0, 1] 
  @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) { 
    Column({ space: 20 }) { 
      Text(tabName).fontSize(18) 
      Image($r('app.media.icon')).width(20).height(20) 
    } 
    .width(100) 
    .height(60) 
    .borderRadius({ topLeft: 10, topRight: 10 }) 
    .onClick(() => { 
      this.controller.changeIndex(tabIndex) 
      this.focusIndex = tabIndex 
    }) 
    .backgroundColor(tabIndex === this.focusIndex ? "#ffffffff" : "#ffb7b7b7") 
  } 
 
  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('80%') 
          .backgroundColor("#ffb7b7b7") 
 
        } 
        .width('100%') 
        .backgroundColor("#ffb7b7b7") 
 
        //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.Blue) 
          }) 
        } 
 
        .barHeight(0) 
        .animationDuration(100) 
        .onChange((index: number) => { 
          console.log('foo change') 
          this.focusIndex = index 
        }) 
      } 
      .alignItems(HorizontalAlign.Start) 
      .width('100%') 
    } 
    .height('100%') 
  } 
}@Entry 
@Component 
struct Index { 
  @State tabArray: Array<number> = [0, 1] 
  @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) { 
    Column({ space: 20 }) { 
      Text(tabName).fontSize(18) 
      Image($r('app.media.icon')).width(20).height(20) 
    } 
    .width(100) 
    .height(60) 
    .borderRadius({ topLeft: 10, topRight: 10 }) 
    .onClick(() => { 
      this.controller.changeIndex(tabIndex) 
      this.focusIndex = tabIndex 
    }) 
    .backgroundColor(tabIndex === this.focusIndex ? "#ffffffff" : "#ffb7b7b7") 
  } 
 
  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('80%') 
          .backgroundColor("#ffb7b7b7") 
 
        } 
        .width('100%') 
        .backgroundColor("#ffb7b7b7") 
 
        //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.Blue) 
          }) 
        } 
 
        .barHeight(0) 
        .animationDuration(100) 
        .onChange((index: number) => { 
          console.log('foo change') 
          this.focusIndex = index 
        }) 
      } 
      .alignItems(HorizontalAlign.Start) 
      .width('100%') 
    } 
    .height('100%') 
  } 
}
分享
微博
QQ
微信
回复
2024-08-05 12:26:59
相关问题
RelativeContainer如何多个view居中
1299浏览 • 1回复 待解决
HarmonyOS Tabs怎么与text同行显示
307浏览 • 1回复 待解决
HarmonyOS RichEditor 内容不居中显示
152浏览 • 1回复 待解决
HarmonyOS Navigation标题居中,背景设置
887浏览 • 1回复 待解决
HarmonyOS Tabs 控件 底部显示不全
30浏览 • 1回复 待解决
Tab导航栏tabbar子组件突出上沿显示
2248浏览 • 1回复 待解决