HarmonyOS Tabs组件,tabBar宽度及tabContent宽度问题

我想实现的功能要求TabContent宽度不受Tabs组件宽度影响,默认情况下tabBar宽度是屏幕宽度,上滑有一个磁吸效果,tabBar宽度变窄,但是TabContent宽度一直都是屏幕宽度。但以现在HarmonyOS的实现方式来看,我找不到一个合适的方法来实现。

HarmonyOS
2024-10-29 11:56:26
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
FengTianYa

​当前Tabs组件规格不支持,可以使用List+ListItemGroup的方式实现

List:

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-container-list-V5

ListItemGroup:

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-container-listitemgroup-V5

demo参考:​

@Entry  
@Component  
struct ListExample {  
  private arr: number[] = [0]  
  @State totalScrollOffset: number = 0;  
  private tabArray: number[] = [0, 1, 2, 3, 4, 5, 6]  
  @State focusIndex: number = 0;  
  private controller: TabsController = new TabsController()  
  @State showIcon: boolean = false  
  
  @Builder  
  myScroll() {  
    Row() {  
      if (this.showIcon) {  
        Image($r('app.media.app_icon'))// TabBar 左侧的元素  
          .width(50)  
          .margin({ right: 20 })  
      }  
      Scroll() {  
        Row() {  
          ForEach(this.tabArray, (item: number, index: number) => {  
            Row({ space: 20 }) {  
              Text('页签' + item)  
                .fontWeight(index === this.focusIndex ? FontWeight.Bold : FontWeight.Normal)  
            }  
            .padding({ left: '10fp', right: '10fp' })  
            .onClick(() => {  
              this.controller.changeIndex(index)  
              this.focusIndex = index  
            })  
          })  
        }  
      }  
      .align(Alignment.Start)  
      .scrollable(ScrollDirection.Horizontal)  
      .scrollBar(BarState.Off)  
    }  
    .width('100%')  
    .height(50)  
    .backgroundColor(Color.White)  
  }  
  build() {  
    Column() {  
      List({ space: 20, initialIndex: 0 }) {  
        ListItem() {  
          Text('header')  
            .width('100%')  
            .height(100)  
            .fontSize(16)  
            .textAlign(TextAlign.Center)  
            .borderRadius(10)  
            .backgroundColor(0xFFFFFF)  
        }  
        ListItemGroup({ header: this.myScroll() }) {  
          ListItem() {  
            //tabs组件把tabbar隐藏  
            Tabs({ barPosition: BarPosition.Start, controller: this.controller }) {  
              ForEach(this.tabArray, (item: number, index: number) => {  
                TabContent() {  
                  Text('我是页面 ' + item + " 的内容")  
                  .fontSize(30)  
                }  
                .backgroundColor(Color.Orange)  
                .width('100%')  
              })  
            }  
            .barHeight(20)  
          }  
        }  
      }  
      .sticky(StickyStyle.Header)  
      .listDirection(Axis.Vertical) // 排列方向  
      .scrollBar(BarState.Off)  
      .edgeEffect(EdgeEffect.None)  
      .onScrollIndex((firstIndex: number, lastIndex: number, centerIndex: number) => {  
        if (firstIndex === 1) {  
          this.showIcon = true  
        } else {  
          this.showIcon = false  
        }  
        console.info('first' + firstIndex)  
        console.info('last' + lastIndex)  
        console.info('center' + centerIndex)  
      })  
      .width('95%')  
    }  
    .width('100%')  
    .height('100%')  
    .backgroundColor(0xDCDCDC)  
    .padding({ top: 5 })  
  
  }  
}
  • 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.
分享
微博
QQ
微信
回复
2024-10-29 16:38:46
相关问题
HarmonyOS Tabs组件tabBar宽度问题
1401浏览 • 1回复 待解决
HarmonyOS Tabs组件宽度问题
627浏览 • 1回复 待解决
HarmonyOS Flex组件宽度问题
1042浏览 • 1回复 待解决
Tabs组件TabContent滑到边缘问题
1065浏览 • 0回复 待解决
HarmonyOS 关于Tabs组件TabContent问题
740浏览 • 1回复 待解决
HarmonyOS Popup宽度问题
633浏览 • 1回复 待解决
HarmonyOS 关于动态设置组件宽度问题
1178浏览 • 1回复 待解决
HarmonyOS 折叠屏webview宽度问题
1149浏览 • 1回复 待解决
HarmonyOS Tabs 组件无法隐藏 tabbar
1427浏览 • 1回复 待解决
HarmonyOS Tabs组件怎么动态添加TabContent
1052浏览 • 1回复 待解决
HarmonyOS组件超出父组件宽度
874浏览 • 1回复 待解决
关于Tabs里面tabBar样式问题
1029浏览 • 2回复 待解决