Tabs的tabbar在Scrollable的模式下,如何实现整体tabbar的位置靠左对齐?

Tabs的tabbar在Scrollable的模式下,如何实现整体tabbar的位置靠左对齐

阅读 852
avatarAI BotBETA

在Scrollable的模式下,要实现Tabs的tabbar整体靠左对齐,你可以通过CSS样式进行设置。具体来说,你可以使用以下CSS样式:

.tabs-scrollable .tabbar {
  justify-content: flex-start;
}

这段CSS代码的作用是将.tabs-scrollable类下的.tabbar元素的对齐方式设置为flex-start,即靠左对齐。你可以将这段CSS代码添加到你的样式表中,或者将其应用到你想要实现靠左对齐的特定元素上。请注意,你需要根据你的实际需求和CSS框架进行调整和适配。

1 个回答

示例代码

// xxx.ets 
@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) { 
    Row({ space: 20 }) { 
      Text(tabName).fontSize(18) 
      Image($r('app.media.icon')).width(20).height(20) 
    } 
    .justifyContent(FlexAlign.Center) 
    .constraintSize({ minWidth: 35 }) 
    .width(120) 
    .height(30) 
    .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('90%') 
          .backgroundColor('#FFB7B7B7') 
 
          Image($r('app.media.ic_empty')).width(20).height(20) 
 
        } 
        .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('#1890FF52') 
          }) 
        } 
 
        .barHeight(0) 
        .animationDuration(100) 
        .onChange((index: number) => { 
          this.focusIndex = index; 
        }) 
      } 
      .alignItems(HorizontalAlign.Start) 
      .width('100%') 
    } 
    .height('100%') 
  } 
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进