Tabs如何才能居左显示呢

需要将tab靠近屏幕显示,但不管怎么设置都是居中的,是不支持嘛?

HarmonyOS
2024-06-04 00:22:37
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
走在河边捡到鱼

用自定义tab的方式实现。

示例代码

@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-06-04 23:00:56
相关问题
HarmonyOS tabs的tabBar怎么
240浏览 • 1回复 待解决
HarmonyOS 顶部tabs如何设置对齐
104浏览 • 1回复 待解决
【JS】如何实现滑删除功能?
3136浏览 • 1回复 待解决
mysql连接的一个问题
2024浏览 • 1回复 待解决
Tab组件,无法对齐该怎么处理?
103浏览 • 1回复 待解决
HarmonyOS Tabs如何取消弹簧效果
104浏览 • 1回复 待解决
HarmonyOS Tabs组件嵌套Tabs组件问题
333浏览 • 1回复 待解决
如何自定义模拟Tabs组件
630浏览 • 1回复 待解决
如何选择Navigation 组件与 Tabs 组件
2199浏览 • 1回复 待解决