1# 选项卡 (Tabs) 2<!--Kit: ArkUI--> 3<!--Subsystem: ArkUI--> 4<!--Owner: @CCFFWW--> 5<!--Designer: @yangfan229--> 6<!--Tester: @lxl007--> 7<!--Adviser: @HelloCrease--> 8 9 10当页面信息较多时,为了让用户能够聚焦于当前显示的内容,需要对页面内容进行分类,提高页面空间利用率。[Tabs](../reference/apis-arkui/arkui-ts/ts-container-tabs.md)组件可以在一个页面内快速实现视图内容的切换,一方面提升查找信息的效率,另一方面精简用户单次获取到的信息量。 11 12 13## 基本布局 14 15 Tabs组件的页面组成包含两个部分,分别是TabContent和TabBar。TabContent是内容页,TabBar是导航页签栏,页面结构如下图所示,根据不同的导航类型,布局会有区别,可以分为底部导航、顶部导航、侧边导航,其导航栏分别位于底部、顶部和侧边。 16 17 **图1** Tabs组件布局示意图 18 19 20 21 22>**说明:** 23> 24> - TabContent组件不支持设置通用宽度属性,其宽度默认撑满Tabs父组件。 25> 26> - TabContent组件不支持设置通用高度属性,其高度由Tabs父组件高度与TabBar组件高度决定。 27 28 29Tabs使用花括号包裹TabContent,如图2,其中TabContent显示相应的内容页。 30 31 32 **图2** Tabs与TabContent使用 33 34 35 36 37每一个TabContent对应的内容需要有一个页签,可以通过TabContent的tabBar属性进行配置。在如下TabContent组件上设置tabBar属性,可以设置其对应页签中的内容,tabBar作为内容的页签。 38 39```ts 40 TabContent() { 41 Text('首页的内容').fontSize(30) 42 } 43.tabBar('首页') 44``` 45 46 47设置多个内容时,需在Tabs内按照顺序放置。 48 49```ts 50Tabs() { 51 TabContent() { 52 Text('首页的内容').fontSize(30) 53 } 54 .tabBar('首页') 55 56 TabContent() { 57 Text('推荐的内容').fontSize(30) 58 } 59 .tabBar('推荐') 60 61 TabContent() { 62 Text('发现的内容').fontSize(30) 63 } 64 .tabBar('发现') 65 66 TabContent() { 67 Text('我的内容').fontSize(30) 68 } 69 .tabBar("我的") 70} 71``` 72 73 74## 底部导航 75 76底部导航是应用中最常见的一种导航方式。底部导航位于应用一级页面的底部,用户打开应用,能够分清整个应用的功能分类,以及页签对应的内容,并且其位于底部更加方便用户单手操作。底部导航一般作为应用的主导航形式存在,其作用是将用户关心的内容按照功能进行分类,迎合用户使用习惯,方便在不同模块间的内容切换。 77 78 79 **图3** 底部导航栏 80 81 82 83 84导航栏位置使用Tabs的barPosition参数进行设置。默认情况下,导航栏位于顶部,此时,barPosition为BarPosition.Start。设置为底部导航时,需要将barPosition设置为BarPosition.End。 85 86 87```ts 88Tabs({ barPosition: BarPosition.End }) { 89 // TabContent的内容:首页、发现、推荐、我的 90 // ... 91} 92``` 93 94底部导航栏可通过设置TabContent的[BottomTabBarStyle](../reference/apis-arkui/arkui-ts/ts-container-tabcontent.md#bottomtabbarstyle9)来实现底部页签样式,详细示例请参考:[示例9(设置底部页签使用symbol图标)](../reference/apis-arkui/arkui-ts/ts-container-tabcontent.md#示例9设置底部页签使用symbol图标)。 95 96 97## 顶部导航 98 99当内容分类较多,用户对不同内容的浏览概率相差不大,需要经常快速切换时,一般采用顶部导航模式进行设计,作为对底部导航内容的进一步划分,常见一些资讯类应用对内容的分类为关注、视频、数码,或者主题应用中对主题进行进一步划分为图片、视频、字体等。 100 101 **图4** 顶部导航栏 102 103 104 105 106```ts 107Tabs({ barPosition: BarPosition.Start }) { 108 // TabContent的内容:关注、视频、游戏、数码、科技、体育、影视 109 // ... 110} 111``` 112 113 114## 侧边导航 115 116侧边导航是应用较为少见的一种导航模式,更多适用于横屏界面,用于对应用进行导航操作,由于用户的视觉习惯是从左到右,侧边导航栏默认为左侧侧边栏。 117 118 119 **图5** 侧边导航栏 120 121 122 123 124实现侧边导航栏需要将Tabs的vertical属性设置为true,vertical默认值为false,表明内容页和导航栏垂直方向排列。 125 126 127 128```ts 129Tabs({ barPosition: BarPosition.Start }) { 130 // TabContent的内容:首页、发现、推荐、我的 131 // ... 132} 133.vertical(true) 134.barWidth(100) 135.barHeight(200) 136``` 137 138 139>**说明:** 140> 141> - vertical为false时,tabbar的宽度默认为撑满屏幕的宽度,需要设置barWidth为合适值。 142> 143> - vertical为true时,tabbar的高度默认为实际内容的高度,需要设置barHeight为合适值。 144 145 146## 限制导航栏的滑动切换 147 148 默认情况下,导航栏都支持滑动切换,在一些内容信息量需要进行多级分类的页面,如支持底部导航+顶部导航组合的情况下,底部导航栏的滑动效果与顶部导航出现冲突,此时需要限制底部导航的滑动,避免引起不好的用户体验。 149 150 **图6** 限制底部导航栏滑动 151 152 153 154 155控制滑动切换的属性为scrollable,默认值为true,表示可以滑动,若要限制滑动切换页签则需要设置为false。 156 157```ts 158Tabs({ barPosition: BarPosition.End }) { 159 TabContent(){ 160 Column(){ 161 Tabs(){ 162 // 顶部导航栏内容 163 // ... 164 } 165 } 166 .backgroundColor('#ff08a8f1') 167 .width('100%') 168 } 169 .tabBar('首页') 170 171 // 其他TabContent内容:发现、推荐、我的 172 // ... 173} 174.scrollable(false) 175``` 176 177 178## 固定导航栏 179 180当内容分类较为固定且不具有拓展性时,例如底部导航内容分类一般固定,分类数量一般在3-5个,此时使用固定导航栏。固定导航栏不可滚动,无法被拖拽滚动,内容均分tabBar的宽度。 181 182 183 **图7** 固定导航栏 184 185 186 187 188Tabs的barMode属性用于控制导航栏是否可以滚动,默认值为BarMode.Fixed。 189 190```ts 191Tabs({ barPosition: BarPosition.End }) { 192 // TabContent的内容:首页、发现、推荐、我的 193 // ... 194} 195.barMode(BarMode.Fixed) 196``` 197 198 199## 滚动导航栏 200 201滚动导航栏可以用于顶部导航栏或者侧边导航栏的设置,内容分类较多,屏幕宽度无法容纳所有分类页签的情况下,需要使用可滚动的导航栏,支持用户点击和滑动来加载隐藏的页签内容。 202 203 204 **图8** 可滚动导航栏 205 206 207 208 209滚动导航栏需要设置Tabs组件的barMode属性,默认值为BarMode.Fixed表示为固定导航栏,BarMode.Scrollable表示可滚动导航栏。 210 211```ts 212Tabs({ barPosition: BarPosition.Start }) { 213 // TabContent的内容:关注、视频、游戏、数码、科技、体育、影视、人文、艺术、自然、军事 214 // ... 215} 216.barMode(BarMode.Scrollable) 217``` 218 219 220## 自定义导航栏 221 222对于底部导航栏,一般作为应用主页面功能区分,为了更好的用户体验,会组合文字以及对应语义图标表示页签内容,这种情况下,需要自定义导航页签的样式。 223 224 225 **图9** 自定义导航栏 226 227 228 229 230系统默认情况下采用了下划线标志当前活跃的页签,而自定义导航栏需要自行实现相应的样式,用于区分当前活跃页签和未活跃页签。 231 232 233设置自定义导航栏需要使用tabBar的参数,以其支持的CustomBuilder的方式传入自定义的函数组件样式。例如这里声明tabBuilder的自定义函数组件,传入参数包括页签文字title,对应位置index,以及选中状态和未选中状态的图片资源。通过当前活跃的currentIndex和页签对应的targetIndex匹配与否,决定UI显示的样式。 234 235```ts 236@State currentIndex: number = 0; 237 238@Builder tabBuilder(title: string, targetIndex: number, selectedImg: Resource, normalImg: Resource) { 239 Column() { 240 Image(this.currentIndex === targetIndex ? selectedImg : normalImg) 241 .size({ width: 25, height: 25 }) 242 Text(title) 243 .fontColor(this.currentIndex === targetIndex ? '#1698CE' : '#6B6B6B') 244 } 245 .width('100%') 246 .height(50) 247 .justifyContent(FlexAlign.Center) 248} 249``` 250 251 252在TabContent对应tabBar属性中传入自定义函数组件,并传递相应的参数。 253 254```ts 255TabContent() { 256 Column(){ 257 Text('我的内容') 258 } 259 .width('100%') 260 .height('100%') 261 .backgroundColor('#007DFF') 262} 263.tabBar(this.tabBuilder('我的', 0, $r('app.media.mine_selected'), $r('app.media.mine_normal'))) 264``` 265 266 267## 切换至指定页签 268 269在不使用自定义导航栏时,默认的Tabs会实现切换逻辑。在使用了自定义导航栏后,默认的Tabs仅实现滑动内容页和点击页签时内容页的切换逻辑,页签切换逻辑需要自行实现。即用户滑动内容页和点击页签时,页签栏需要同步切换至内容页对应的页签。 270 271 272 **图10** 内容页和页签不联动 273 274 275 276此时需要使用Tabs提供的onSelected事件方法,监听索引index的变化,并将选中元素的index值传递给selectIndex,实现页签的切换。 277 278```ts 279@Entry 280@Component 281struct TabsExample1 { 282 @State selectIndex: number = 0; 283 @Builder tabBuilder(title: string, targetIndex: number) { 284 Column() { 285 Text(title) 286 .fontColor(this.selectIndex === targetIndex ? '#1698CE' : '#6B6B6B') 287 } 288 } 289 290 build() { 291 Column() { 292 Tabs({ barPosition: BarPosition.End }) { 293 TabContent() { 294 Text("首页内容").width('100%').height('100%').backgroundColor('rgb(213,213,213)') 295 .fontSize(40).fontColor(Color.Black).textAlign(TextAlign.Center) 296 }.tabBar(this.tabBuilder('首页', 0)) 297 298 TabContent() { 299 Text("发现内容").width('100%').height('100%').backgroundColor('rgb(112,112,112)') 300 .fontSize(40).fontColor(Color.Black).textAlign(TextAlign.Center) 301 }.tabBar(this.tabBuilder('发现', 1)) 302 303 TabContent() { 304 Text("推荐内容").width('100%').height('100%').backgroundColor('rgb(39,135,217)') 305 .fontSize(40).fontColor(Color.Black).textAlign(TextAlign.Center) 306 }.tabBar(this.tabBuilder('推荐', 2)) 307 308 TabContent() { 309 Text("我的内容").width('100%').height('100%').backgroundColor('rgb(0,74,175)') 310 .fontSize(40).fontColor(Color.Black).textAlign(TextAlign.Center) 311 }.tabBar(this.tabBuilder('我的', 3)) 312 } 313 .animationDuration(0) 314 .backgroundColor('#F1F3F5') 315 .onSelected((index: number) => { 316 this.selectIndex = index; 317 }) 318 }.width('100%') 319 } 320} 321``` 322 **图11** 内容页和页签联动 323 324 325 326若希望不滑动内容页和点击页签也能实现内容页和页签的切换,可以将currentIndex传给Tabs的index参数,通过改变currentIndex来实现跳转至指定索引值对应的TabContent内容。也可以使用TabsController,TabsController是Tabs组件的控制器,用于控制Tabs组件进行内容页切换。通过TabsController的changeIndex方法来实现跳转至指定索引值对应的TabContent内容。 327```ts 328@State currentIndex: number = 2; 329@State currentAnimationMode: AnimationMode = AnimationMode.CONTENT_FIRST; 330private controller: TabsController = new TabsController(); 331 332Tabs({ barPosition: BarPosition.End, index: this.currentIndex, controller: this.controller }) { 333 // ... 334} 335.height(600) 336.animationMode(this.currentAnimationMode) 337.onChange((index: number) => { 338 this.currentIndex = index; 339}) 340 341Button('动态修改AnimationMode').width('50%').margin({ top: 1 }).height(25) 342 .onClick(()=>{ 343 if (this.currentAnimationMode === AnimationMode.CONTENT_FIRST) { 344 this.currentAnimationMode = AnimationMode.ACTION_FIRST; 345 } else if (this.currentAnimationMode === AnimationMode.ACTION_FIRST) { 346 this.currentAnimationMode = AnimationMode.NO_ANIMATION; 347 } else if (this.currentAnimationMode === AnimationMode.NO_ANIMATION) { 348 this.currentAnimationMode = AnimationMode.CONTENT_FIRST_WITH_JUMP; 349 } else if (this.currentAnimationMode === AnimationMode.CONTENT_FIRST_WITH_JUMP) { 350 this.currentAnimationMode = AnimationMode.ACTION_FIRST_WITH_JUMP; 351 } else if (this.currentAnimationMode === AnimationMode.ACTION_FIRST_WITH_JUMP) { 352 this.currentAnimationMode = AnimationMode.CONTENT_FIRST; 353 } 354}) 355 356Button('动态修改index').width('50%').margin({ top: 20 }) 357 .onClick(()=>{ 358 this.currentIndex = (this.currentIndex + 1) % 4; 359}) 360 361Button('changeIndex').width('50%').margin({ top: 20 }) 362 .onClick(()=>{ 363 let index = (this.currentIndex + 1) % 4; 364 this.controller.changeIndex(index); 365}) 366``` 367 368 **图12** 切换指定页签 369 370 371 372开发者可以通过Tabs组件的onContentWillChange接口,设置自定义拦截回调函数。拦截回调函数在下一个页面即将展示时被调用,如果回调返回true,新页面可以展示;如果回调返回false,新页面不会展示,仍显示原来页面。 373 374```ts 375Tabs({ barPosition: BarPosition.End, controller: this.controller, index: this.currentIndex }) { 376 // ... 377 } 378 .onContentWillChange((currentIndex, comingIndex) => { 379 if (comingIndex == 2) { 380 return false; 381 } 382 return true; 383 }) 384``` 385 **图13** 支持开发者自定义页面切换拦截事件 386 387 388<!--Del--> 389## 支持适老化 390 391在适老化大字体场景下,底部页签提供大字体弹窗显示内容。当组件识别到大字体时,基于设置的文字和图标等内容,构建长按提示弹窗。当用户长按弹窗后,滑动到下一个页签位置时,使用新页签的弹窗提示内容替换上一个页签提示内容,抬手关闭弹窗并切换到对应TabContent内容页。 392 393> **说明:** 394> 395> 弹窗只适用于底部页签BottomTabBarStyle。 396 397**图14** 在适老化场景下通过长按底部页签显示适老化弹窗。 398 399 400 401```ts 402import { abilityManager, Configuration } from '@kit.AbilityKit'; 403import { BusinessError } from '@kit.BasicServicesKit'; 404import { uiAppearance } from '@kit.ArkUI'; 405 406@Entry 407@Component 408struct Demo { 409 @State fontColor: string = '#182431'; 410 @State selectedFontColor: string = '#007DFF'; 411 @State currentIndex: number = 0; 412 @State currentFontSizeScale: string = ''; 413 @State showBuilderTab: boolean = false; 414 @State fontSize: number = 15; 415 private darkModeKey: string[] = Object.keys(uiAppearance.DarkMode).filter( 416 key => typeof uiAppearance.DarkMode[key] === 'number'); 417 418 async setFontScale(scale: number): Promise<void> { 419 let configInit: Configuration = { 420 fontSizeScale: scale, 421 }; 422 abilityManager.updateConfiguration(configInit, (err: BusinessError) => { 423 if (err) { 424 console.error(`updateConfiguration fail, err: ${JSON.stringify(err)}`); 425 this.getUIContext().getPromptAction().showToast({ message: `scale:${scale}, err:${JSON.stringify(err)}` }); 426 } else { 427 this.currentFontSizeScale = String(scale); 428 if (scale > 1) { 429 this.fontSize = 8; 430 } else { 431 this.fontSize = 15; 432 } 433 console.log('updateConfiguration success.'); 434 this.getUIContext().getPromptAction().showToast({ message: `scale:${scale}, updateConfiguration success.` }); 435 } 436 }); 437 } 438 439 darkMode(isDarkMode: boolean): void { 440 let mode: uiAppearance.DarkMode = uiAppearance.DarkMode.ALWAYS_LIGHT; 441 if (isDarkMode) { 442 mode = uiAppearance.DarkMode.ALWAYS_DARK; 443 } 444 if (mode == uiAppearance.getDarkMode()) { 445 console.info(`TitleDarkMode Set ${this.darkModeKey[mode]} successfully.`); 446 return; 447 } 448 try { 449 uiAppearance.setDarkMode(mode).then(() => { 450 console.info(`TitleDarkMode Set ${this.darkModeKey[mode]} successfully.`); 451 }).catch((error: Error) => { 452 console.error(`TitleDarkMode Set ${this.darkModeKey[mode]} failed, ${error.message}`); 453 }); 454 } catch (error) { 455 let message = (error as BusinessError).message; 456 console.error(`TitleDarkMode Set dark-mode failed, ${message}`); 457 } 458 } 459 460 build() { 461 Column() { 462 Column() { 463 Row() { 464 Text(`current fontSizeScale:${this.currentFontSizeScale}`) 465 .margin({ top: 5, bottom: 5 }) 466 .fontSize(this.fontSize) 467 } 468 469 Row() { 470 Button('1.75') 471 .margin({ top: 5, bottom: 5 }) 472 .fontSize(this.fontSize) 473 .width('40%') 474 .onClick(async () => { 475 await this.setFontScale(1.75); 476 }) 477 Button('2') 478 .margin({ top: 5, bottom: 5 }) 479 .fontSize(this.fontSize) 480 .width('40%') 481 .onClick(async () => { 482 await this.setFontScale(2); 483 }) 484 }.margin({ top: 25 }) 485 486 Row() { 487 Button('3.2') 488 .margin({ top: 5, bottom: 5 }) 489 .fontSize(this.fontSize) 490 .width('40%') 491 .onClick(async () => { 492 await this.setFontScale(3.2); 493 }) 494 Button('1') 495 .margin({ top: 5, bottom: 5 }) 496 .fontSize(this.fontSize) 497 .width('40%') 498 .onClick(async () => { 499 await this.setFontScale(1); 500 }) 501 } 502 503 Row() { 504 Button('深色模式') 505 .margin({ top: 5, bottom: 5 }) 506 .fontSize(this.fontSize) 507 .width('40%') 508 .onClick(async () => { 509 this.darkMode(true); 510 }) 511 Button('浅色模式') 512 .margin({ top: 5, bottom: 5 }) 513 .fontSize(this.fontSize) 514 .width('40%') 515 .onClick(async () => { 516 this.darkMode(false); 517 }) 518 } 519 }.alignItems(HorizontalAlign.Start) 520 521 Column() { 522 Tabs({ barPosition: BarPosition.End }) { 523 TabContent() { 524 Column().width('100%').height('100%').backgroundColor(Color.Pink) 525 }.tabBar(new BottomTabBarStyle($r('sys.media.ohos_app_icon'), 'OverLength')) 526 TabContent() { 527 Column().width('100%').height('100%').backgroundColor(Color.Yellow) 528 }.tabBar(new BottomTabBarStyle($r('sys.media.ohos_app_icon'), 'SixLine')) 529 TabContent() { 530 Column().width('100%').height('100%').backgroundColor(Color.Blue) 531 }.tabBar(new BottomTabBarStyle($r('sys.media.ohos_app_icon'), 'Blue')) 532 TabContent() { 533 Column().width('100%').height('100%').backgroundColor(Color.Green) 534 }.tabBar(new BottomTabBarStyle($r('sys.media.ohos_app_icon'), 'Green')) 535 } 536 .vertical(false) 537 .scrollable(true) 538 .barMode(BarMode.Fixed) 539 .onChange((index: number) => { 540 console.info(index.toString()); 541 }) 542 .width('100%') 543 .backgroundColor(0xF1F3F5) 544 }.width('80%').height(200) 545 .margin({ top: 200 }) 546 }.width('100%') 547 } 548} 549``` 550<!--DelEnd--> 551 552## 相关实例 553 554如需详细了解Tabs的更多实现,请参考以下示例: 555 556- [常用组件与布局](https://gitee.com/openharmony/codelabs/tree/master/ETSUI/ArkTSComponents) 557<!--RP1--><!--RP1End-->