HarmonyOS Tabs 容器如果 只有两个tab 时候,如何让tab 置顶,而不是默认居中显示?

Tabs 容器如果 只有两个tab 时候,如何让tab 置顶 咨询场景描述:Tabs 容器如果 只有两个tab 时候,默认是居中的。UI 需要置顶

HarmonyOS
2024-08-10 11:33:20
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zxjiu

这个是tabs高度未确定,定高即可,参考代码段

Tabs({ barPosition: BarPosition.Start }) {   // TabContent的内容:关注、视频、游戏、数码、科技、体育、影视   ... }
  • 1.

参考链接https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/arkts-navigation-tabs-0000001820999773#ZH-CN_TOPIC_0000001820999773__%E9%A1%B6%E9%83%A8%E5%AF%BC%E8%88%AA

@Entry 
@Component 
struct TabsExample11 { 
  @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%') 
  } 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.

需要tabbar靠左的话 建议可以通过自定义tabbar来实现可以参考以上demo解决

分享
微博
QQ
微信
回复
2024-08-10 18:00:23
相关问题
HarmonyOS Tab居中问题
621浏览 • 1回复 待解决
HarmonyOS Tab指定默认Index
633浏览 • 1回复 待解决
如何Swiper在一屏中显示两个Item?
2216浏览 • 1回复 待解决
HarmonyOS 怎么两个list同时滚动
733浏览 • 1回复 待解决
HarmonyOS 怎么配置tab默认选择的tabBar
1063浏览 • 1回复 待解决
如何判断两个日期是不是同一天?
1504浏览 • 1回复 待解决