希望Tabs的页签位置是否支持自定义

希望Tabs的页签位置是否支持自定义。

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

可以在TabContent的tabBar属性中自定义TabBar显示内容的布局样式(设置为左对齐),类型为CustomBuilder。详细文档请参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-container-tabcontent-V5

以下代码供您参考:

@Entry  
@Component  
struct TabsExample {  
  @State fontColor: string = '#182431'  
  @State selectedFontColor: string = '#007DFF'  
  @State currentIndex: number = 0  
  private controller: TabsController = new TabsController()  
  
  @Builder tabBuilder(index: number, name: string) {  
    Column() {  
      Flex({ direction: FlexDirection.Row }){  
        Text(name)  
          .fontColor(this.currentIndex === index ? this.selectedFontColor : this.fontColor)  
          .fontSize(16)  
          .fontWeight(this.currentIndex === index ? 500 : 400)  
          .lineHeight(22)  
          .margin({ top: 17, bottom: 7 })  
      }  
      Divider()  
        .strokeWidth(2)  
        .color('#007DFF')  
        .opacity(this.currentIndex === index ? 1 : 0)  
    }.width('100%')  
  }  
  
  build() {  
    Column() {  
      Tabs({ barPosition: BarPosition.Start, index: this.currentIndex, controller: this.controller }) {  
        TabContent() {  
          Column().width('100%').height('100%').backgroundColor('#00CB87')  
        }.tabBar(this.tabBuilder(0, 'green'))  
  
        TabContent() {  
          Column().width('100%').height('100%').backgroundColor('#007DFF')  
        }.tabBar(this.tabBuilder(1, 'blue'))  
      }  
      .vertical(false)  
      .barMode(BarMode.Fixed)  
      .barWidth(360)  
      .barHeight(56)  
      .animationDuration(400)  
      .onChange((index: number) => {  
        this.currentIndex = index  
      })  
      .width(360)  
      .height(296)  
      .margin({ top: 52 })  
      .backgroundColor('#F1F3F5')  
    }.width('100%')  
  }  
}  
@Entry  
@Component  
struct TabDemo5 {  
  @State tabArray: Array<number> = [0, 1]  
  @State focusIndex: number = 0  
  @State index: number = 0  
  private controller: TabsController = new TabsController()  
  
  // 自定义单独的页签  
  @Builder  
  Tab(tabName: string, tabItem: number, tabIndex: number) {  
    Column() {  
      Text(tabName).fontSize(18)  
    }  
    .width(100)  
    .height(40)  
    .borderRadius({ topLeft: 10, topRight: 10 })  
    .onClick(() => {  
      this.controller.changeIndex(tabIndex)  
      this.focusIndex = tabIndex  
    })  
    .backgroundColor(tabIndex === this.focusIndex ? "#ffffffff" : "#ffb7b7b7")  
    .justifyContent(FlexAlign.Center)  
  }  
  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('100%')  
          .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('#ffbdf38d')  
          })  
        }  
        .barHeight(0)  
        // .animationDuration(100)  
        .onChange((index: number) => {  
          console.log('foo change')  
          this.focusIndex = index  
        })  
      }  
      .alignItems(HorizontalAlign.Start)  
      .width('100%')  
    }  
    .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.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
分享
微博
QQ
微信
回复
2024-10-11 16:28:47
相关问题
鸿蒙Tabs跳转错误
280浏览 • 0回复 待解决
是否支持自定义装饰器
2870浏览 • 1回复 待解决
Grid组件scrollBar是否支持自定义
3113浏览 • 1回复 待解决
自定义组件是否支持renderFit属性
2621浏览 • 1回复 待解决
HarmonyOS ArkTS是否支持自定义注解
960浏览 • 1回复 待解决
ArkTS是否支持自定义装饰器?
3572浏览 • 1回复 待解决
HarmonyOS 是否支持自定义装饰器?
1025浏览 • 1回复 待解决
HarmonyOS 组件是否支持自定义事件
836浏览 • 1回复 待解决
HarmonyOS ArkWeb是否支持自定义UserAgent
1231浏览 • 1回复 待解决
HarmonyOS 是否支持自定义升级弹窗
708浏览 • 1回复 待解决
HarmonyOS 希望优化自定义弹窗使用
926浏览 • 1回复 待解决
如何自定义模拟Tabs组件
1724浏览 • 1回复 待解决
如何设置自定义弹窗位置
2824浏览 • 1回复 待解决
弹窗打开、关闭动画是否支持自定义
3354浏览 • 1回复 待解决