当前位置: 首页> 游戏> 攻略 > HarmonyOS开发之Tab样式(背景高亮样式)

HarmonyOS开发之Tab样式(背景高亮样式)

时间:2025/7/12 6:29:38来源:https://blog.csdn.net/qq_28189091/article/details/142066161 浏览次数:0次

一:开发环境

二:效果图

三:实现步骤

@Entry
@Component
struct TabsPage {@State tabArray:string[] = ["首页","分类","应用","热点","我的"]@State focusIndex: number = 0;@State index: number = 0;private controller: TabsController = new TabsController();private listScroller: Scroller = new Scroller();private barHeight: number = 0;@BuildertabBuilder(tabName: string, tabIndex: number) {Row() {Text(tabName).fontSize(14).fontColor(tabIndex === this.focusIndex ? "#638EEF": "#E6000000").id(tabIndex.toString())}.justifyContent(FlexAlign.Center).width(60).backgroundColor(tabIndex === this.focusIndex ? "#243b75ef" : "").borderRadius(5).height(30).margin({ left: 8}).onClick(() => {this.controller.changeIndex(tabIndex)//  this.listScroller.scrollToIndex(tabIndex, true, ScrollAlign.CENTER);this.listScroller.scrollToIndex(tabIndex)})}build() {Column() {Stack().height(30)Row() {List({ scroller: this.listScroller }) {ForEach(this.tabArray,(item: string, index: number)=>{this.tabBuilder(item, index)},(item: string, index: number) => JSON.stringify(item) + index)}.width('100%').height('100%').listDirection(Axis.Horizontal).scrollBar(BarState.Off)}.width('95%').alignItems(VerticalAlign.Center).height(50).margin({ top: 8 })Tabs({ barPosition: BarPosition.Start, controller: this.controller }) {ForEach(this.tabArray,(item: string) => {TabContent() {Row() {Text(item).height(300).fontSize(30)}.width('100%').justifyContent(FlexAlign.Center)}.backgroundColor(Color.White)}, (item: string, index: number) => JSON.stringify(item) + index)}.width("100%").barHeight(this.barHeight).animationDuration(300).onChange((targetIndex: number)=>{this.focusIndex = targetIndexthis.listScroller.scrollToIndex(targetIndex)})// .onAnimationStart((index: number, targetIndex: number) => {//api 9以长//   console.log( 'index', index.toString());//   this.focusIndex = targetIndex;//   this.listScroller.scrollToIndex(targetIndex, true, ScrollAlign.CENTER);// })}}
}

关键字:HarmonyOS开发之Tab样式(背景高亮样式)

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: