完整示例参考如下:
@Entry
@Component
struct TabsMask {
fontColor: string = '#000000';
selectedFontColor: string = '#007DFF';
@State currentIndex: number = 0;
@State selectedIndex: number = 0;
private controller: TabsController = new TabsController();
@Builder
tabBuilder(index: number, name: string) {
Column() {
Text(name)
.fontColor(this.selectedIndex === index ? this.selectedFontColor : this.fontColor)
.fontSize(16)
.fontWeight(this.selectedIndex === index ? 500 : 400)
.lineHeight(22)
.margin({ top: 17, bottom: 7 });
Divider()
.strokeWidth(2)
.color(this.selectedFontColor)
.opacity(this.selectedIndex === index ? 1 : 0);
}
.width('25%');
}
@Builder
overlayBuilder() {
Stack()
.height('100%')
.width('100%')
.linearGradient({
// 渐变方向
direction: GradientDirection.Left,
// 数组末尾元素占比小于1时满足重复着色效果
colors: [['#40ffffff', 0.0], ['#26ffffff', 0.1]]
})
.hitTestBehavior(HitTestMode.None)
.height(56);
Stack()
.height('100%')
.width('100%')
.linearGradient({
// 渐变方向
direction: GradientDirection.Right,
// 数组末尾元素占比小于1时满足重复着色效果
colors: [['#40ffffff', 0.0], ['#26ffffff', 0.1]]
})
.hitTestBehavior(HitTestMode.None)
.height(56);
}
build() {
Column() {
Tabs({ barPosition: BarPosition.Start, index: this.currentIndex, controller: this.controller }) {
TabContent() {
Column()
.width('100%')
.height('100%')
.backgroundColor('#FF0A59f7');
}
.tabBar(this.tabBuilder(0, '热点'));
TabContent() {
Column()
.width('100%')
.height('100%')
.backgroundColor('#E50A59F7');
}
.tabBar(this.tabBuilder(1, '电视剧'));
TabContent() {
Column()
.width('100%')
.height('100%')
.backgroundColor('#B20A59F7');
}
.tabBar(this.tabBuilder(2, '电影'));
TabContent() {
Column()
.width('100%')
.height('100%')
.backgroundColor('#990A59F7');
}
.tabBar(this.tabBuilder(3, '短剧'));
TabContent() {
Column()
.width('100%')
.height('100%')
.backgroundColor('#7F0A59F7');
}
.tabBar(this.tabBuilder(4, '综艺'));
TabContent() {
Column()
.width('100%')
.height('100%')
.backgroundColor('#660A59F7');
}
.tabBar(this.tabBuilder(5, '动漫'));
TabContent() {
Column()
.width('100%')
.height('100%')
.backgroundColor('#4D0A59F7');
}
.tabBar(this.tabBuilder(6, '纪录片'));
}
.overlay(this.overlayBuilder())
.vertical(false)
.barMode(BarMode.Scrollable)
.barWidth(360)
.barHeight(56)
.animationDuration(400)
.onChange((index: number) => {
this.currentIndex = index;
this.selectedIndex = index;
})
.onAnimationStart((index: number, targetIndex: number, event: TabsAnimationEvent) => {
if (index === targetIndex) {
return;
}
console.info(event.toString());
this.selectedIndex = targetIndex;
})
.width(360)
.height(296)
.margin({ top: 52 })
.backgroundColor('#ffd1d1d6');
}
.width('100%');
}
}