方案一:利用组件的position属性自定义每一个TabItem的位置。
@Builder
TabItem(tabName: string, tabIndex: number) {
// 标签页项的布局
Column({ space: 10 }) {
Column() {
Image($r('app.media.startIcon'))
.width(32).height(32)
.margin({ top: 5 });
Text(tabName).fontSize(14)
.margin({ top: 5 });
}
.width('100%')
.height('10%')
.justifyContent(FlexAlign.Center);
}
.justifyContent(FlexAlign.Center)
.onClick(() => {
this.controller.changeIndex(tabIndex);
this.focusIndex = tabIndex;
})
.width('12%')
.padding({ bottom: 10 })
.backgroundColor(Color.White)
.position( // 设置TabItem的相对位置
tabIndex === 1 ? { left: 80 } :
tabIndex === 2 ? { right: 90 } :
tabIndex === 3 ? { right: 0 } : {});
} // 用Row组件实现TabBar效果
Row() {
ForEach(this.tabArray, (item: number, index: number) => {
this.TabItem('页签 ' + item, index);
});
}
.justifyContent(FlexAlign.SpaceBetween)
.alignItems(VerticalAlign.Bottom)
.width('100%')
.backgroundColor(Color.White); 完整示例参考如下:
@Entry
@Component
export struct CusTab2 {
@State tabArray: Array<number> = [0, 1, 2, 3];
@State focusIndex: number = 0;
private controller: TabsController = new TabsController();
@Builder
TabItem(tabName: string, tabIndex: number) {
// 标签页项的布局
Column({ space: 10 }) {
Column() {
Image($r('app.media.startIcon'))
.width(32).height(32)
.margin({ top: 5 });
Text(tabName).fontSize(14)
.margin({ top: 5 });
}
.width('100%')
.height('10%')
.justifyContent(FlexAlign.Center);
}
.justifyContent(FlexAlign.Center)
.onClick(() => {
this.controller.changeIndex(tabIndex);
this.focusIndex = tabIndex;
})
.width('12%')
.padding({ bottom: 10 })
.backgroundColor(Color.White)
.position( // 设置TabItem的相对位置
tabIndex === 1 ? { left: 80 } :
tabIndex === 2 ? { right: 90 } :
tabIndex === 3 ? { right: 0 } : {});
}
build() {
Column() {
Stack({ alignContent: Alignment.BottomStart }) {
// Tabs
Tabs({ barPosition: BarPosition.End, controller: this.controller }) {
ForEach(this.tabArray, (item: number) => {
TabContent() {
Text('我是页面 ' + item + ' 的内容')
.height(10)
.width('100%')
.fontSize(30)
.textAlign(TextAlign.Center);
}
.backgroundColor(Color.White);
});
}
.barHeight(0)
.animationDuration(100)
.onChange((index: number) => {
console.info('foo change');
this.focusIndex = index;
});
// 用Row组件实现TabBar效果
Row() {
ForEach(this.tabArray, (item: number, index: number) => {
this.TabItem('页签 ' + item, index);
});
}
.justifyContent(FlexAlign.SpaceBetween)
.alignItems(VerticalAlign.Bottom)
.width('100%')
.backgroundColor(Color.White);
}
.width('100%');
}
.height('100%');
}
} 效果预览:

@Builder
Tab(tabName: string, tabIndex: number) {
Column({ space: 10 }) {
Stack() {
Column() {
Image($r('app.media.startIcon'))
.width(32).height(32);
Text(tabName).fontSize(14)
.margin({ top: 5 });
}
.width('100%')
.justifyContent(FlexAlign.Center);
}
.margin({ bottom: tabIndex === 2 ? 40 : 0 });
}
.justifyContent(FlexAlign.Center)
.onClick(() => {
this.controller.changeIndex(tabIndex);
this.focusIndex = tabIndex;
})
.width(tabIndex === 2 ? '28%' : '18%')
// 左右四栏设置宽度占比18%,中间构造异形导航宽度占比28%
.padding({ top: 10, bottom: 10 })
.backgroundColor(Color.White)
.borderRadius(tabIndex === 2 ? { topLeft: '50%', topRight: '50%' } : {});
} 完整示例参考如下:
@Entry
@Component
export struct CusTab2 {
@State tabArray: Array<number> = [0, 1, 2, 3, 4];
@State focusIndex: number = 0;
private controller: TabsController = new TabsController();
@Builder
Tab(tabName: string, tabIndex: number) {
Column({ space: 10 }) {
Stack() {
Column() {
Image($r('app.media.startIcon'))
.width(32).height(32);
Text(tabName).fontSize(14)
.margin({ top: 5 });
}
.width('100%')
.justifyContent(FlexAlign.Center);
}
.margin({ bottom: tabIndex === 2 ? 40 : 0 });
}
.justifyContent(FlexAlign.Center)
.onClick(() => {
this.controller.changeIndex(tabIndex);
this.focusIndex = tabIndex;
})
.width(tabIndex === 2 ? '28%' : '18%')
// 左右四栏设置宽度占比18%,中间构造异形导航宽度占比28%
.padding({ top: 10, bottom: 10 })
.backgroundColor(Color.White)
.borderRadius(tabIndex === 2 ? { topLeft: '50%', topRight: '50%' } : {});
}
build() {
Column() {
Stack({ alignContent: Alignment.BottomStart }) {
// tabs
Tabs({ barPosition: BarPosition.End, controller: this.controller }) {
ForEach(this.tabArray, (item: number) => {
TabContent() {
Text('我是页面 ' + item + ' 的内容')
.height(10)
.width('100%')
.fontSize(30)
.textAlign(TextAlign.Center);
}
.backgroundColor('#F1F3F5');
});
}
.barHeight(0)
.animationDuration(100)
.onChange((index: number) => {
console.info('foo change');
this.focusIndex = index;
}); // 页签
Row() {
ForEach(this.tabArray, (item: number, index: number) => {
this.Tab('页签 ' + item, index);
});
}
.justifyContent(FlexAlign.SpaceBetween)
.alignItems(VerticalAlign.Bottom)
.width('100%');
}
.width('100%');
}
.height('100%');
}
} 效果预览:
