HarmonyOS Tabs设置水平滚动显示,tabItem较少时怎么居左显示

Tabs设置.scrollable(true) .barMode(BarMode.Scrollable),tabItem较少时怎么居左显示(默认居中显示的)。

HarmonyOS
2024-09-24 11:24:22
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
superinsect

较少时可参考:

@Entry  
@Component  
struct Index {  
  @State tabArray: Array<number> = [0,1,2]  
  @State focusIndex: number = 0  
  private controller: TabsController = new TabsController()  
  scrollerForList: Scroller = new Scroller()  
  
  // 单独的页签  
  @Builder  
  Tab(tabName: string, tabItem: number, tabIndex: number) {  
    Column({ space: 20 }) {  
      Text(tabName).fontSize(18)  
    }  
    .width(100)  
    .height(30)  
    .borderRadius({ topLeft: 10, topRight: 10 })  
    .onClick(() => {  
      this.controller.changeIndex(tabIndex)  
      this.focusIndex = tabIndex  
    })  
    .backgroundColor(tabIndex === this.focusIndex ? "#ffffffff" : "#ffb7b7b7")  
  }  
  
  build() {  
    Column() {  
      // 页签  
      Row() {  
        List({ scroller: this.scrollerForList }) {  
          ForEach(this.tabArray, (item: number, index: number) => {  
            ListItem() {  
              this.Tab("页" + item, item, index)  
            }  
          })  
        }  
        .listDirection(Axis.Horizontal)  
        .scrollBar(BarState.Off)  
      }  
      .height(30)  
      .justifyContent(FlexAlign.Start)  
      .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)  
        })  
      }  
      .barMode(BarMode.Scrollable)  
      .barHeight(0)  
      .animationDuration(100)  
      .onChange((index: number) => {  
        this.focusIndex = index  
        // 使用scroller实现上下联动,具体滚动效果根据业务需要自行设计,以下为滚动示例  
        this.scrollerForList.scrollToIndex(index,true)  
      })  
    }  
    .alignItems(HorizontalAlign.Start)  
    .width('100%')  
    .height('100%')  
  }  
}
分享
微博
QQ
微信
回复
2024-09-24 15:52:53
相关问题
Tabs如何才能显示
712浏览 • 1回复 待解决
HarmonyOS tabs的tabBar怎么
240浏览 • 1回复 待解决
HarmonyOS 顶部tabs如何设置对齐
104浏览 • 1回复 待解决
请问ScrollView怎么显示滚动条?
5325浏览 • 1回复 待解决
过长文字如何滚动显示
1670浏览 • 1回复 待解决
ListContainer 有滚动显示吗?
5450浏览 • 1回复 待解决
HarmonyOS Tabs 的bar 怎么设置对齐左边
299浏览 • 1回复 待解决
Rect设置渐变显示异常
54浏览 • 0回复 待解决
过长文字如何滚动显示,有人知道吗?
1786浏览 • 1回复 待解决