TabBar标签栏

位于 APP 底部,方便用户在不同功能模块之间进行快速切换。

规则

  • 用作 APP 的一级分类,数量控制在 3-5 个之间。
  • 即使某个 Tab 不可用,也不要禁用或者移除该 Tab。
  • 使用 Badge 进行提示,足不出户也能知道有内容更新。

代码演示

APP 型选项卡

多用于页面的内容区块,起着控制小范围内的大块内容的分组和隐藏,起着保持界面整洁的作用。其中Tabbar可以显示在顶部或者底部。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'demo-tab-bar-basic',
  4. template: `
  5. <TabBar
  6. [hidden]="hidden"
  7. [barTintColor]="'white'"
  8. [tintColor]="tintColor"
  9. [ngStyle]="tabbarStyle"
  10. [activeTab]="selectedIndex"
  11. [unselectedTintColor]="unselectedTintColor"
  12. [tabBarPosition]="topFlag ? 'top' : 'bottom'"
  13. (onPress)="tabBarTabOnPress($event)"
  14. >
  15. <TabBarItem [title]="'Life'" [key]="1" [badge]="1" [icon]="icon1" [selectedIcon]="icon11">
  16. <ng-template #icon1>
  17. <div
  18. style="width:22px;height: 22px;background: url('https://zos.alipayobjects.com/rmsportal/sifuoDUQdAFKAVcFGROC.svg') center center / 21px 21px no-repeat;"
  19. ></div>
  20. </ng-template>
  21. <ng-template #icon11>
  22. <div
  23. style="width:22px;height: 22px;background: url('https://zos.alipayobjects.com/rmsportal/iSrlOTqrKddqbOmlvUfq.svg') center center / 21px 21px no-repeat;"
  24. ></div>
  25. </ng-template>
  26. <div style="background-color: white; height: 100%; text-align: center">
  27. <div style="padding-top: 60px">Clicked Life tab, show Life information</div>
  28. <ng-container>
  29. <ng-template [ngTemplateOutlet]="content"></ng-template>
  30. </ng-container>
  31. </div>
  32. </TabBarItem>
  33. <TabBarItem [title]="'Koubei'" [key]="2" [badge]="'new'" [icon]="icon2" [selectedIcon]="icon22">
  34. <ng-template #icon2>
  35. <div
  36. style="width:22px;height: 22px;background: url('https://gw.alipayobjects.com/zos/rmsportal/BTSsmHkPsQSPTktcXyTV.svg') center center / 21px 21px no-repeat;"
  37. ></div>
  38. </ng-template>
  39. <ng-template #icon22>
  40. <div
  41. style="width:22px;height: 22px;background: url('https://gw.alipayobjects.com/zos/rmsportal/ekLecvKBnRazVLXbWOnE.svg') center center / 21px 21px no-repeat;"
  42. ></div>
  43. </ng-template>
  44. <div style="background-color: white; height: 100%; text-align: center">
  45. <div style="padding-top: 60px">Clicked Koubei tab, show Koubei information</div>
  46. <ng-container>
  47. <ng-template [ngTemplateOutlet]="content"></ng-template>
  48. </ng-container>
  49. </div>
  50. </TabBarItem>
  51. <TabBarItem [title]="'Friend'" [key]="3" [dot]="true" [icon]="icon3" [selectedIcon]="icon33">
  52. <ng-template #icon3>
  53. <div
  54. style="width:22px;height: 22px;background: url('https://zos.alipayobjects.com/rmsportal/psUFoAMjkCcjqtUCNPxB.svg') center center / 21px 21px no-repeat;"
  55. ></div>
  56. </ng-template>
  57. <ng-template #icon33>
  58. <div
  59. style="width:22px;height: 22px;background: url('https://zos.alipayobjects.com/rmsportal/IIRLrXXrFAhXVdhMWgUI.svg') center center / 21px 21px no-repeat;"
  60. ></div>
  61. </ng-template>
  62. <div style="background-color: white; height: 100%; text-align: center">
  63. <div style="padding-top: 60px">Clicked Friend tab, show Friend information</div>
  64. <ng-container>
  65. <ng-template [ngTemplateOutlet]="content"></ng-template>
  66. </ng-container>
  67. </div>
  68. </TabBarItem>
  69. <TabBarItem [title]="'My'" [key]="4" [icon]="icon4" [selectedIcon]="icon44">
  70. <ng-template #icon4>
  71. <div
  72. style="width:22px;height: 22px;background: url('https://zos.alipayobjects.com/rmsportal/asJMfBrNqpMMlVpeInPQ.svg') center center / 21px 21px no-repeat;"
  73. ></div>
  74. </ng-template>
  75. <ng-template #icon44>
  76. <div
  77. style="width:22px;height: 22px;background: url('https://zos.alipayobjects.com/rmsportal/gjpzzcrPMkhfEqgbYvmN.svg') center center / 21px 21px no-repeat;"
  78. ></div>
  79. </ng-template>
  80. <div style="background-color: white; height: 100%; text-align: center">
  81. <div style="padding-top: 60px">Clicked My tab, show My information</div>
  82. <ng-container>
  83. <ng-template [ngTemplateOutlet]="content"></ng-template>
  84. </ng-container>
  85. </div>
  86. </TabBarItem>
  87. </TabBar>
  88. <ng-template #content>
  89. <a style="display: block; margin-top: 40px; margin-bottom: 20px; color: #108ee9" (click)="showNextTabBar($event)">
  90. Click to next tab-bar
  91. </a>
  92. <a style="display: block; margin-top: 20px; margin-bottom: 20px; color: #108ee9" (click)="showTabBar($event)">
  93. Click to show/hide tab-bar
  94. </a>
  95. <a style="display: block; margin-top: 20px; margin-bottom: 20px; color: #108ee9" (click)="changePosition($event)">
  96. Click to change tab-bar position top/bottom
  97. </a>
  98. <a style="display: block; margin-bottom: 60px; color: #108ee9" (click)="showFullScreen($event)">
  99. Click to switch fullscreen
  100. </a>
  101. </ng-template>
  102. `
  103. })
  104. export class DemoTabBarBasicComponent {
  105. hidden: boolean = false;
  106. fullScreen: boolean = false;
  107. topFlag: boolean = false;
  108. tintColor: string = '#108ee9';
  109. unselectedTintColor: string = '#888';
  110. tabbarStyle: object = { height: '400px' };
  111. selectedIndex: number = 1;
  112. showTabBar(event) {
  113. event.preventDefault();
  114. this.hidden = !this.hidden;
  115. }
  116. showNextTabBar(event) {
  117. event.preventDefault();
  118. const PANE_COUNT = 4;
  119. if (this.selectedIndex == PANE_COUNT - 1) {
  120. this.selectedIndex = 0;
  121. } else {
  122. this.selectedIndex++;
  123. }
  124. console.log('selectedIndex: ', this.selectedIndex);
  125. }
  126. showFullScreen(event) {
  127. event.preventDefault();
  128. this.fullScreen = !this.fullScreen;
  129. this.tabbarStyle = this.fullScreen
  130. ? {
  131. position: 'fixed',
  132. height: '100%',
  133. width: '100%',
  134. top: 0
  135. }
  136. : { height: '400px' };
  137. }
  138. changePosition(event) {
  139. event.preventDefault();
  140. this.topFlag = !this.topFlag;
  141. }
  142. tabBarTabOnPress(pressParam: any) {
  143. console.log('onPress Params: ', pressParam);
  144. this.selectedIndex = pressParam.index;
  145. }
  146. }

TabBar标签栏 - 图1

API

TabBar

参数说明类型默认值
[activeTab]当前激活Tab索引number0
[barTintColor]tabbar 背景色string‘white’
[tintColor]选中的字体颜色string‘#108ee9’
[unselectedTintColor]未选中的字体颜色string‘#888’
[hidden]是否隐藏booleanfalse
[tabBarPosition]tabbar 位置‘top’|’bottom’‘bottom’
[prerenderingSiblingsNumber]预加载两侧Tab数量, -1: 加载所有的tab内容, 0: 仅加载当前tab内容, n: 预加载两侧n个Tabnumber-1
(onPress)bar 点击触发EventEmitter<{index: number, title: string, key: string}>-

TabBarItem

参数说明类型默认值
[badge]徽标数number | string-
[dot]是否在右上角显示小红点(在设置badge的情况下失效)booleanfalse
[icon]默认展示的内容TemplateRef-
[selectedIcon]选中后展示的内容TemplateRef-
[title]标题文字string-
[key]唯一标识string-