【HarmonyOS NEXT】如何实现TabBar盖在TabContent上面

 

【关键字】

TabBar / TabContent / 凸起 / 叠加

【问题描述】

期待的UI效果是底部tab栏中间要凸起,需要盖在content上面。效果图如下所示:

cke_445.png

当前看代码,TabContent和TabBar都是同一层级,有什么方法能支持TabBar盖在TabContent上面?

【解决方案】

整体思路是自定义了一个Tab叠加在页面中,可以按需求自己实现Tab。

示例Demo如下:

@Entry
@Component
struct Drag2 {
@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

@State icon:string ='app.media.icon'

@Builder
Tab(tabName: string, tabItem: number, tabIndex: number) {
Column({ space: 10 }) {
Stack(){
Image($r('app.media.startIcon'))
.width(tabIndex === 1 ? 40 : 20).height(tabIndex === 1? 40 : 20)
Text(tabName).fontSize(18)
.margin({top:tabIndex === 1 ? 130 : 50})
}
.margin({bottom:tabIndex ===1 ? 80 : 0})
}
.justifyContent(FlexAlign.Center)
.onClick(() => {
this.controller.changeIndex(tabIndex)
this.focusIndex = tabIndex
})
.backgroundColor(Color.White)
.width(120)
.height(100)
}

build() {
Column() {
Stack({alignContent:Alignment.BottomStart}) {
//tabs
Tabs({ barPosition: BarPosition.End, 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
})
// 页签
Row() {
Scroll() {
Row() {
ForEach(this.tabArray, (item: number, index: number) => {
this.Tab("页签 " + item, item, index)
})
}
.margin({ top:20 })
.justifyContent(FlexAlign.SpaceBetween)
.alignItems(VerticalAlign.Bottom)
}
.scrollable(ScrollDirection.Horizontal)
.scrollBar(BarState.Off)
.width('100%')
}
.justifyContent(FlexAlign.SpaceBetween)
.alignItems(VerticalAlign.Bottom)
.width('100%')
}
.width('100%')
}
.height('100%')
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值