@Entry
@Component
struct Index {
@State
private currentIndex: number = 0;
private controller: TabsController = new TabsController();
@State change: boolean = true;
// tabBar组件
@Builder
tabBuilder(index: number, name: string) {
RelativeContainer() {
Text(name)
.fontColor(this.currentIndex === index ? '#0a59f7' : '#182431')
.fontSize(16)
.fontWeight(this.currentIndex === index ? 500 : 400)
.height('auto')
.padding({
left: 8,
right: 8,
top: 6,
bottom: 6
})
.id('textTitle')
.alignRules({
middle: { anchor: '__container__', align: HorizontalAlign.Center },
center: { anchor: '__container__', align: VerticalAlign.Center }
});
Divider()
.strokeWidth(2)
.color('#0a59f7')
.opacity(this.currentIndex === index ? 1 : 0)
.width(100)
.alignRules({ bottom: { anchor: '__container__', align: VerticalAlign.Bottom } });
}
.width(100);
}
build() {
RelativeContainer() {
Tabs({ barPosition: BarPosition.Start, index: this.currentIndex, controller: this.controller }) {
TabContent() {
Text('页面一');
}.tabBar(this.tabBuilder(0, '页面一'));
TabContent() {
Text('页面二,点击显隐页面三')
.onClick(() => {
this.change = !this.change;
});
}
.tabBar(this.tabBuilder(1, '页面二'));
if (this.change) {
TabContent() {
Text('页面三');
}
.tabBar(this.tabBuilder(2, '页面三'));
TabContent() {
Text('页面四');
}
.tabBar(this.tabBuilder(3, '页面四'));
TabContent() {
Text('页面五');
}
.tabBar(this.tabBuilder(4, '页面五'));
} else {
// 当隐藏页面后需要保证TabContent页面的第一个参数Index连续
TabContent() {
Text('页面四');
}
.tabBar(this.tabBuilder(2, '页面四'));
TabContent() {
Text('页面五');
}
.tabBar(this.tabBuilder(3, '页面五'));
}
}
.barMode(BarMode.Scrollable)
.onChange((index) => {
this.currentIndex = index;
})
.animationDuration(400)
.scrollable(true)
.vertical(false)
.width('100%')
.fadingEdge(false);
};
}
} 实现效果如下:

interface Info {
id: number;
name: string;
}
@Entry
@Component
struct PageTwo {
private controller: TabsController = new TabsController();
@State currentIndex: number = 0;
@State colorArray: Info[] = [
{ id: 0, name: '首页' },
{ id: 1, name: '新品' },
{ id: 2, name: '社区' },
{ id: 3, name: '购物车' },
{ id: 4, name: '我的' },
];
@Builder
tabBuilder(index: number, name: string) {
RelativeContainer() {
Text(name)
.fontColor(this.currentIndex === index ? '#0a59f7' : '#182431')
.fontSize(16)
.fontWeight(this.currentIndex === index ? 500 : 400)
.height('auto')
.padding({
left: 8,
right: 8,
top: 6,
bottom: 6
})
.id('textTitle')
.alignRules({
middle: { anchor: '__container__', align: HorizontalAlign.Center },
center: { anchor: '__container__', align: VerticalAlign.Center }
});
Divider()
.strokeWidth(2)
.color('#0a59f7')
.opacity(this.currentIndex === index ? 1 : 0)
.width(100)
.alignRules({ bottom: { anchor: '__container__', align: VerticalAlign.Bottom } });
}
.width(100);
}
build() {
Tabs({ barPosition: BarPosition.Start, index: this.currentIndex, controller: this.controller }) {
ForEach(this.colorArray, (item: Info, index: number) => {
TabContent() {
if (index === 2) {
Text('点击显隐最后一个页面')
.onClick(() => {
if (this.colorArray.length === 5) {
this.colorArray.splice(this.colorArray.length - 1, 1);
} else {
this.colorArray.push({ id: 4, name: '我的' });
}
});
} else {
Text(`页面${index}`);
}
}.tabBar(this.tabBuilder(index, item.name));
});
}
.onChange((index) => {
this.currentIndex = index;
});
}
} 实现效果如下:
