@Component
export struct expenditure {
@State focusIndex: number = 0;
private controller: TabsController = new TabsController();
tabArray = [‘全部’, ‘获得’,‘消费’];
// 自定义页签
@Builder
Tab(tabName: string, tabItem: number, tabIndex: number) {
Column({ space: 20 }) {
Text(tabName).fontSize(18)
}.alignItems(HorizontalAlign.Center)
.width(60)
.height(25)
.onClick(() => {
this.controller.changeIndex(tabIndex);
this.focusIndex = tabIndex;
})
.backgroundColor(tabIndex === this.focusIndex ? ‘#ffb7b7b7’:‘#ffffffff’)
}
build() {
Column() {
Column() {
// 页签
Row({ space: 6 }) {
Scroll() {
Row() {
ForEach(this.tabArray, (item: number, index: number) => {
this.Tab(‘’ + item, item, index);
})
}
.justifyContent(FlexAlign.Start)
}
// 设置左对齐
.align(Alignment.Start)
.scrollable(ScrollDirection.Horizontal)
.scrollBar(BarState.Off)
.width(‘80%’)
}
.width('100%')
// .backgroundColor('#ffb7b7b7')
// tabs
Tabs({ barPosition: BarPosition.Start, controller: this.controller }) {
ForEach(this.tabArray, (item: number, index: number) => {
TabContent() {
if(index==0){
Text('1')
}else if(index==1){
Text('2')
}else if(index==2){
Text('3')
}
}
})
}
.barHeight(0)
.animationDuration(100)
.onChange((index: number) => {
console.log('foo change');
this.focusIndex = index;
})
}
.alignItems(HorizontalAlign.Start)
.width('100%')
}
.height('100%')
}
}