您的位置:首页 > 教育 > 培训 > 南宁互联网公司前十名_金沙集团186cc成色_军事新闻最新24小时_服务器域名查询

南宁互联网公司前十名_金沙集团186cc成色_军事新闻最新24小时_服务器域名查询

2025/2/26 0:59:33 来源:https://blog.csdn.net/aidou1314/article/details/145060469  浏览:    关键词:南宁互联网公司前十名_金沙集团186cc成色_军事新闻最新24小时_服务器域名查询
南宁互联网公司前十名_金沙集团186cc成色_军事新闻最新24小时_服务器域名查询

 参考网址:自定义页签切换联动

1.自定义组件TabBarView


@Component
export struct TabBarView{@State currentIndex: number = 0@State selectedIndex: number = 0private controller: TabsController = new TabsController()//tab标签内容+横线布局@Builder tabBuilder(index: number, name: string) {Column() {Text(name).fontColor(this.selectedIndex === index ? '#007DFF' : '#182431').fontSize(18).fontWeight(this.selectedIndex === index ? 500 : 400).lineHeight(22).height('95%')Divider().strokeWidth(2).color('#007DFF').opacity(this.selectedIndex === index ? 1 : 0)}.backgroundColor(Color.White).width('100%').height('100%')}@Link arr: Array<string>@Builder tabItem(item: string) {}@BuilderParam item: (item: string) => void = this.tabItembuild() {Column() {Tabs({ barPosition: BarPosition.Start, index: this.currentIndex, controller: this.controller }) {ForEach(this.arr, (item: string, index: number) => {TabContent() {this.item(item)}.tabBar(this.tabBuilder(index, item))}, (item: string, index: number) => index.toString())}.vertical(false).barMode(BarMode.Fixed).barWidth(360).barHeight(56).animationDuration(400).onChange((index: number) => {// currentIndex控制TabContent显示页签this.currentIndex = indexthis.selectedIndex = index}).onAnimationStart((index: number, targetIndex: number, event: TabsAnimationEvent) => {if (index === targetIndex) {return}// selectedIndex控制自定义TabBar内Image和Text颜色切换this.selectedIndex = targetIndex}).width('100%').height('100%').backgroundColor('#F1F3F5')}.width('100%')}
}

2.组件中使用

import { TabBarView } from "./TabBarView"
@Entry({routeName: 'IndexPage'})@Component
export struct IndexPage{@State arr: Array<string> = ['标签1', '标签2']@BuildertabItem(item: string){//在此处写各个标签对于的布局组件,可抽离出来使代码简洁if (item === this.arr[0]){//标签1组件}else if (item === this.arr[1]){//标签2组件}}build() {Column(){TabBarView({arr: this.arr, item: this.tabItem})}}
}

说明:我是根据官网提供的Tabs实例代码1(自定义页签切换联动)封装的简单使用(代码可直接使用),这种方式是为了复用。

版权声明:

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

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