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

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

HarmonyOS
2024-02-20 10:56:35
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
wjmfzsz

示例代码

// 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%') 
  } 
}
分享
微博
QQ
微信
回复
2024-02-20 19:57:54
相关问题
eTS求助 tabBarIcon代码样例 ?
4137浏览 • 1回复 待解决
JS中input值文本对齐如何实现
6453浏览 • 1回复 待解决
Tab导航栏tabbar子组件突出上沿显示
15浏览 • 1回复 待解决
Tabs组件懒加载问题
30浏览 • 1回复 待解决
stagemode 模式如何获取version信息?
497浏览 • 0回复 待解决
如何禁止Tabs系统切换逻辑
36浏览 • 0回复 待解决
Scroll子组件展示位置如何调整
324浏览 • 1回复 待解决