当前位置:   article > 正文

鸿蒙Harmony应用开发—ArkTS声明式开发(容器组件:Tabs)

鸿蒙Harmony应用开发—ArkTS声明式开发(容器组件:Tabs)

通过页签进行内容视图切换的容器组件,每个页签对应一个内容视图。

说明:

该组件从API Version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。

该组件从API Version 11开始默认支持安全区避让特性(默认值为:expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.BOTTOM])),开发者可以重写该属性覆盖默认行为,API Version 11之前的版本需配合expandSafeArea属性实现安全区避让。

子组件

仅可包含子组件TabContent

说明:

Tabs子组件的visibility属性设置为None,或者visibility属性设置为Hidden时,对应子组件不显示,但依然会在视窗内占位。

接口

Tabs(value?: {barPosition?: BarPosition, index?: number, controller?: TabsController})

参数:

参数名参数类型必填参数描述
barPositionBarPosition设置Tabs的页签位置。
默认值:BarPosition.Start
indexnumber设置当前显示页签的索引。
默认值:0
说明:
设置为小于0的值时按默认值显示。
可选值为[0, TabContent子节点数量-1]。
直接修改index跳页时,切换动效不生效。 使用TabController的changeindex时,默认生效切换动效,可以设置animationDuration为0关闭动画。
从API version 10开始,该参数支持$$双向绑定变量。
controllerTabsController设置Tabs控制器。

BarPosition枚举说明

名称描述
Startvertical属性方法设置为true时,页签位于容器左侧;vertical属性方法设置为false时,页签位于容器顶部。
Endvertical属性方法设置为true时,页签位于容器右侧;vertical属性方法设置为false时,页签位于容器底部。

属性

除支持通用属性外,还支持以下属性:

名称参数类型描述
verticalboolean设置为false是为横向Tabs,设置为true时为纵向Tabs。
默认值:false
scrollableboolean设置为true时可以通过滑动页面进行页面切换,为false时不可滑动切换页面。
默认值:true
barModeBarMode,ScrollableBarModeOptionsTabBar布局模式,BarMode为必选项,ScrollableBarModeOptions为可选项,具体描述见BarMode枚举说明、ScrollableBarModeOptions对象说明。从API version 10开始,支持ScrollableBarModeOptions参数。其中ScrollableBarModeOptions参数仅Scrollable模式下有效,非必填参数。
默认值:BarMode.Fixed
barWidthnumber | Length8+TabBar的宽度值。
默认值:
未设置SubTabBarStyleBottomTabBarStyle的TabBar且vertical属性为false时,默认值为Tabs的宽度。
未设置SubTabBarStyleBottomTabBarStyle的TabBar且vertical属性为true时,默认值为56vp。
设置SubTabbarStyle样式且vertical属性为false时,默认值为Tabs的宽度。
设置SubTabbarStyle样式且vertical属性为true时,默认值为56vp。
设置BottomTabbarStyle样式且vertical属性为true时,默认值为96vp。
设置BottomTabbarStyle样式且vertical属性为false时,默认值为Tabs的宽度。
说明:
设置为小于0或大于Tabs宽度值时,按默认值显示。
barHeightnumber | Length8+TabBar的高度值。
默认值:
未设置带样式的TabBar且vertical属性为false时,默认值为56vp。
未设置带样式的TabBar且vertical属性为true时,默认值为Tabs的高度。
设置SubTabbarStyle样式且vertical属性为false时,默认值为56vp。
设置SubTabbarStyle样式且vertical属性为true时,默认值为Tabs的高度。
设置BottomTabbarStyle样式且vertical属性为true时,默认值为Tabs的高度。
设置BottomTabbarStyle样式且vertical属性为false时,默认值为56vp。
说明:
设置为小于0或大于Tabs高度值时,按默认值显示。
animationDurationnumber点击TabBar页签切换TabContent的动画时长。
默认值:
API version 10及以前,不设置该属性时,默认值为0ms,即点击TabBar页签切换TabContent无动画。设置为小于0的异常值时,默认值为300ms。
API version 11及以后,不设置该属性或设置为小于0的异常值,且设置TabBar为BottomTabBarStyle样式时,默认值为0ms。设置TabBar为其他样式时,默认值为300ms。
说明:
该参数不支持百分比设置。
divider10+DividerStyle | null用于设置区分TabBar和TabContent的分割线样式设置分割线样式,默认不显示分割线。
DividerStyle: 分割线的样式;
null: 不显示分割线。
fadingEdge10+boolean设置页签超过容器宽度时是否渐隐消失。
默认值:true
说明:
建议配合barBackgroundColor属性一起使用,如果barBackgroundColor属性没有定义,会默认显示页签末端为白色的渐隐效果。
barOverlap10+boolean设置TabBar是否背后变模糊并叠加在TabContent之上。
默认值:false
barBackgroundColor10+ResourceColor设置TabBar的背景颜色。
默认值:透明
barGridAlign10+BarGridColumnOptions以栅格化方式设置TabBar的可见区域。具体参见BarGridColumnOptions对象。仅水平模式下有效,不适用于XS、XL和XXL设备

DividerStyle10+对象说明

名称参数类型必填描述
strokeWidthLength分割线的线宽(不支持百分比设置)。
colorResourceColor分割线的颜色。
默认值:#33182431
startMarginLength分割线与侧边栏顶端的距离(不支持百分比设置)。
默认值:0.0
单位:vp
endMarginLength分割线与侧边栏底端的距离(不支持百分比设置)。
默认值:0.0
单位:vp

BarGridColumnOptions10+对象说明

名称参数类型必填描述
marginDimension网格模式下的column边距(不支持百分比设置)。
默认值:24.0
单位:vp
gutterDimension网格模式下的column间隔(不支持百分比设置)。
默认值:24.0
单位:vp
smnumber小屏下,页签占用的columns数量,必须是非负偶数。小屏为大于等于320vp但小于600vp。
默认值为-1,代表页签占用TabBar全部宽度。
mdnumber中屏下,页签占用的columns数量,必须是非负偶数。中屏为大于等于600vp但小于800vp。
默认值为-1,代表页签占用TabBar全部宽度。
lgnumber大屏下,页签占用的columns数量,必须是非负偶数。大屏为大于等于840vp但小于1024vp。
默认值为-1,代表页签占用TabBar全部宽度。

ScrollableBarModeOptions10+对象说明

名称参数类型必填描述
marginDimensionScrollable模式下的TabBar的左右边距(不支持百分比设置)。
默认值:0.0
单位:vp
nonScrollableLayoutStyleLayoutStyleScrollable模式下不滚动时的页签排布方式。
默认值:LayoutStyle.ALWAYS_CENTER

BarMode枚举说明

名称描述
Scrollable每一个TabBar均使用实际布局宽度,超过总长度(横向Tabs的barWidth,纵向Tabs的barHeight)后可滑动。
Fixed所有TabBar平均分配barWidth宽度(纵向时平均分配barHeight高度)。

LayoutStyle10+枚举说明

名称描述
ALWAYS_CENTER当页签内容超过TabBar宽度时,TabBar可滚动。
当页签内容不超过TabBar宽度时,TabBar不可滚动,页签紧凑居中。
ALWAYS_AVERAGE_SPLITE当页签内容超过TabBar宽度时,TabBar可滚动。
当页签内容不超过TabBar宽度时,TabBar不可滚动,且所有页签平均分配TabBar宽度。
仅水平模式下有效,否则视为LayoutStyle.ALWAYS_CENTER。
SPACE_BETWEEN_OR_CENTER当页签内容超过TabBar宽度时,TabBar可滚动。
当页签内容不超过TabBar宽度但超过TabBar宽度一半时,TabBar不可滚动,页签紧凑居中。
当页签内容不超过TabBar宽度一半时,TabBar不可滚动,保证页签居中排列在TabBar宽度一半,且间距相同。

事件

除支持通用事件外,还支持以下事件:

名称功能描述
onChange(event: (index: number) => void)Tab页签切换后触发的事件。
- index:当前显示的index索引,索引从0开始计算。
触发该事件的条件:
1、TabContent支持滑动时,组件触发滑动时触发。
2、通过控制器API接口调用。
3、通过状态变量构造的属性值进行修改。
4、通过页签处点击触发。
onTabBarClick(event: (index: number) => void)10+Tab页签点击后触发的事件。
- index:被点击的index索引,索引从0开始计算。
触发该事件的条件:
通过页签处点击触发。
onAnimationStart11+(handler: (index: number, targetIndex: number, event: TabsAnimationEvent) => void)切换动画开始时触发该回调。
- index:当前显示元素的索引。
- targetIndex:切换动画目标元素的索引。
- event:动画相关信息,包括主轴方向上当前显示元素和目标元素相对Tabs起始位置的位移,以及离手速度。
说明:
参数为动画开始前的index值(不是最终结束动画的index值)。
onAnimationEnd11+(handler: (index: number, event: TabsAnimationEvent) => void)切换动画结束时触发该回调。
- index:当前显示元素的索引。
- event:动画相关信息,只返回主轴方向上当前显示元素相对于Tabs起始位置的位移。
说明:
当Tabs切换动效结束时触发,包括动画过程中手势中断。参数为动画结束后的index值。
onGestureSwipe11+(handler: (index: number, event: TabsAnimationEvent) => void)在页面跟手滑动过程中,逐帧触发该回调。
- index:当前显示元素的索引。
- event:动画相关信息,只返回主轴方向上当前显示元素相对于Tabs起始位置的位移。
customContentTransition11+(delegate: (from: number, to: number) => TabContentAnimatedTransition | undefined)自定义Tabs页面切换动画。其中,from和to参数为返回给开发者使用的值,代表的含义如下:
- from:动画开始时,当前页面的index值。
- to:动画开始时,目标页面的index值。
使用说明:
1、当使用自定义切换动画时,Tabs组件自带的默认切换动画会被禁用,同时,页面也无法跟手滑动。
2、当设置为undefined时,表示不使用自定义切换动画,仍然使用组件自带的默认切换动画。
3、当前自定义切换动画不支持打断。
4、目前自定义切换动画只支持两种场景触发:点击页签和调用TabsController.changeIndex()接口。
5、当使用自定义切换动画时,Tabs组件支持的事件中,除了onGestureSwipe,其他事件均支持。
6、onChange和onAnimationEnd事件的触发时机需要特殊说明:如果在第一次自定义动画执行过程中,触发了第二次自定义动画,那么在开始第二次自定义动画时,就会触发第一次自定义动画的onChange和onAnimationEnd事件。
7、当使用自定义动画时,参与动画的页面布局方式会改为Stack布局。如果开发者未主动设置相关页面的zIndex属性,那么所有页面的zIndex值是一样的,页面的渲染层级会按照在组件树上的顺序(即页面的index值顺序)确定。因此,开发者需要主动修改页面的zIndex属性,来控制页面的渲染层级。

TabsController

Tabs组件的控制器,用于控制Tabs组件进行页签切换。不支持一个TabsController控制多个Tabs组件。

导入对象

let controller: TabsController = new TabsController()

changeIndex

changeIndex(value: number): void

控制Tabs切换到指定页签。

参数:

参数名参数类型必填参数描述
valuenumber页签在Tabs里的索引值,索引值从0开始。
说明:
设置小于0或大于最大数量的值时,取默认值0。

示例

示例1

本示例通过onChange实现切换时自定义tabBar和TabContent的联动。

  1. // xxx.ets
  2. @Entry
  3. @Component
  4. struct TabsExample {
  5. @State fontColor: string = '#182431'
  6. @State selectedFontColor: string = '#007DFF'
  7. @State currentIndex: number = 0
  8. private controller: TabsController = new TabsController()
  9. @Builder tabBuilder(index: number, name: string) {
  10. Column() {
  11. Text(name)
  12. .fontColor(this.currentIndex === index ? this.selectedFontColor : this.fontColor)
  13. .fontSize(16)
  14. .fontWeight(this.currentIndex === index ? 500 : 400)
  15. .lineHeight(22)
  16. .margin({ top: 17, bottom: 7 })
  17. Divider()
  18. .strokeWidth(2)
  19. .color('#007DFF')
  20. .opacity(this.currentIndex === index ? 1 : 0)
  21. }.width('100%')
  22. }
  23. build() {
  24. Column() {
  25. Tabs({ barPosition: BarPosition.Start, index: this.currentIndex, controller: this.controller }) {
  26. TabContent() {
  27. Column().width('100%').height('100%').backgroundColor('#00CB87')
  28. }.tabBar(this.tabBuilder(0, 'green'))
  29. TabContent() {
  30. Column().width('100%').height('100%').backgroundColor('#007DFF')
  31. }.tabBar(this.tabBuilder(1, 'blue'))
  32. TabContent() {
  33. Column().width('100%').height('100%').backgroundColor('#FFBF00')
  34. }.tabBar(this.tabBuilder(2, 'yellow'))
  35. TabContent() {
  36. Column().width('100%').height('100%').backgroundColor('#E67C92')
  37. }.tabBar(this.tabBuilder(3, 'pink'))
  38. }
  39. .vertical(false)
  40. .barMode(BarMode.Fixed)
  41. .barWidth(360)
  42. .barHeight(56)
  43. .animationDuration(400)
  44. .onChange((index: number) => {
  45. this.currentIndex = index
  46. })
  47. .width(360)
  48. .height(296)
  49. .margin({ top: 52 })
  50. .backgroundColor('#F1F3F5')
  51. }.width('100%')
  52. }
  53. }

tabs2

示例2

本示例通过divider实现了分割线各种属性的展示。

  1. // xxx.ets
  2. @Entry
  3. @Component
  4. struct TabsDivider1 {
  5. private controller1: TabsController = new TabsController()
  6. @State dividerColor: string = 'red'
  7. @State strokeWidth: number = 2
  8. @State startMargin: number = 0
  9. @State endMargin: number = 0
  10. @State nullFlag: boolean = false
  11. build() {
  12. Column() {
  13. Tabs({ controller: this.controller1 }) {
  14. TabContent() {
  15. Column().width('100%').height('100%').backgroundColor(Color.Pink)
  16. }.tabBar('pink')
  17. TabContent() {
  18. Column().width('100%').height('100%').backgroundColor(Color.Yellow)
  19. }.tabBar('yellow')
  20. TabContent() {
  21. Column().width('100%').height('100%').backgroundColor(Color.Blue)
  22. }.tabBar('blue')
  23. TabContent() {
  24. Column().width('100%').height('100%').backgroundColor(Color.Green)
  25. }.tabBar('green')
  26. TabContent() {
  27. Column().width('100%').height('100%').backgroundColor(Color.Red)
  28. }.tabBar('red')
  29. }
  30. .vertical(true)
  31. .scrollable(true)
  32. .barMode(BarMode.Fixed)
  33. .barWidth(70)
  34. .barHeight(200)
  35. .animationDuration(400)
  36. .onChange((index: number) => {
  37. console.info(index.toString())
  38. })
  39. .height('200vp')
  40. .margin({ bottom: '12vp' })
  41. .divider(this.nullFlag ? null : {
  42. strokeWidth: this.strokeWidth,
  43. color: this.dividerColor,
  44. startMargin: this.startMargin,
  45. endMargin: this.endMargin
  46. })
  47. Button('常规Divider').width('100%').margin({ bottom: '12vp' })
  48. .onClick(() => {
  49. this.nullFlag = false;
  50. this.strokeWidth = 2;
  51. this.dividerColor = 'red';
  52. this.startMargin = 0;
  53. this.endMargin = 0;
  54. })
  55. Button('空Divider').width('100%').margin({ bottom: '12vp' })
  56. .onClick(() => {
  57. this.nullFlag = true
  58. })
  59. Button('颜色变为蓝色').width('100%').margin({ bottom: '12vp' })
  60. .onClick(() => {
  61. this.dividerColor = 'blue'
  62. })
  63. Button('宽度增加').width('100%').margin({ bottom: '12vp' })
  64. .onClick(() => {
  65. this.strokeWidth += 2
  66. })
  67. Button('宽度减小').width('100%').margin({ bottom: '12vp' })
  68. .onClick(() => {
  69. if (this.strokeWidth > 2) {
  70. this.strokeWidth -= 2
  71. }
  72. })
  73. Button('上边距增加').width('100%').margin({ bottom: '12vp' })
  74. .onClick(() => {
  75. this.startMargin += 2
  76. })
  77. Button('上边距减少').width('100%').margin({ bottom: '12vp' })
  78. .onClick(() => {
  79. if (this.startMargin > 2) {
  80. this.startMargin -= 2
  81. }
  82. })
  83. Button('下边距增加').width('100%').margin({ bottom: '12vp' })
  84. .onClick(() => {
  85. this.endMargin += 2
  86. })
  87. Button('下边距减少').width('100%').margin({ bottom: '12vp' })
  88. .onClick(() => {
  89. if (this.endMargin > 2) {
  90. this.endMargin -= 2
  91. }
  92. })
  93. }.padding({ top: '24vp', left: '24vp', right: '24vp' })
  94. }
  95. }

tabs3

示例3

本示例通过fadingEdge实现了切换子页签渐隐和不渐隐。

  1. // xxx.ets
  2. @Entry
  3. @Component
  4. struct TabsOpaque {
  5. @State message: string = 'Hello World'
  6. private controller: TabsController = new TabsController()
  7. private controller1: TabsController = new TabsController()
  8. @State selfFadingFade: boolean = true;
  9. build() {
  10. Column() {
  11. Button('子页签设置渐隐').width('100%').margin({ bottom: '12vp' })
  12. .onClick((event?: ClickEvent) => {
  13. this.selfFadingFade = true;
  14. })
  15. Button('子页签设置不渐隐').width('100%').margin({ bottom: '12vp' })
  16. .onClick((event?: ClickEvent) => {
  17. this.selfFadingFade = false;
  18. })
  19. Tabs({ barPosition: BarPosition.End, controller: this.controller }) {
  20. TabContent() {
  21. Column().width('100%').height('100%').backgroundColor(Color.Pink)
  22. }.tabBar('pink')
  23. TabContent() {
  24. Column().width('100%').height('100%').backgroundColor(Color.Yellow)
  25. }.tabBar('yellow')
  26. TabContent() {
  27. Column().width('100%').height('100%').backgroundColor(Color.Blue)
  28. }.tabBar('blue')
  29. TabContent() {
  30. Column().width('100%').height('100%').backgroundColor(Color.Green)
  31. }.tabBar('green')
  32. TabContent() {
  33. Column().width('100%').height('100%').backgroundColor(Color.Green)
  34. }.tabBar('green')
  35. TabContent() {
  36. Column().width('100%').height('100%').backgroundColor(Color.Green)
  37. }.tabBar('green')
  38. TabContent() {
  39. Column().width('100%').height('100%').backgroundColor(Color.Green)
  40. }.tabBar('green')
  41. TabContent() {
  42. Column().width('100%').height('100%').backgroundColor(Color.Green)
  43. }.tabBar('green')
  44. }
  45. .vertical(false)
  46. .scrollable(true)
  47. .barMode(BarMode.Scrollable)
  48. .barHeight(80)
  49. .animationDuration(400)
  50. .onChange((index: number) => {
  51. console.info(index.toString())
  52. })
  53. .fadingEdge(this.selfFadingFade)
  54. .height('30%')
  55. .width('100%')
  56. Tabs({ barPosition: BarPosition.Start, controller: this.controller1 }) {
  57. TabContent() {
  58. Column().width('100%').height('100%').backgroundColor(Color.Pink)
  59. }.tabBar('pink')
  60. TabContent() {
  61. Column().width('100%').height('100%').backgroundColor(Color.Yellow)
  62. }.tabBar('yellow')
  63. TabContent() {
  64. Column().width('100%').height('100%').backgroundColor(Color.Blue)
  65. }.tabBar('blue')
  66. TabContent() {
  67. Column().width('100%').height('100%').backgroundColor(Color.Green)
  68. }.tabBar('green')
  69. TabContent() {
  70. Column().width('100%').height('100%').backgroundColor(Color.Green)
  71. }.tabBar('green')
  72. TabContent() {
  73. Column().width('100%').height('100%').backgroundColor(Color.Green)
  74. }.tabBar('green')
  75. }
  76. .vertical(true)
  77. .scrollable(true)
  78. .barMode(BarMode.Scrollable)
  79. .barHeight(200)
  80. .barWidth(80)
  81. .animationDuration(400)
  82. .onChange((index: number) => {
  83. console.info(index.toString())
  84. })
  85. .fadingEdge(this.selfFadingFade)
  86. .height('30%')
  87. .width('100%')
  88. }
  89. .padding({ top: '24vp', left: '24vp', right: '24vp' })
  90. }
  91. }

tabs4

示例4

本示例通过barOverlap实现了TabBar是否背后变模糊并叠加在TabContent之上。

  1. // xxx.ets
  2. @Entry
  3. @Component
  4. struct barBackgroundColorTest {
  5. private controller: TabsController = new TabsController()
  6. @State barOverlap: boolean = true;
  7. @State barBackgroundColor: string = '#88888888';
  8. build() {
  9. Column() {
  10. Button("barOverlap变化").width('100%').margin({ bottom: '12vp' })
  11. .onClick((event?: ClickEvent) => {
  12. if (this.barOverlap) {
  13. this.barOverlap = false;
  14. } else {
  15. this.barOverlap = true;
  16. }
  17. })
  18. Tabs({ barPosition: BarPosition.Start, index: 0, controller: this.controller }) {
  19. TabContent() {
  20. Column() {
  21. Text(`barOverlap ${this.barOverlap}`).fontSize(16).margin({ top: this.barOverlap ? '56vp' : 0 })
  22. Text(`barBackgroundColor ${this.barBackgroundColor}`).fontSize(16)
  23. }.width('100%').width('100%').height('100%')
  24. .backgroundColor(Color.Pink)
  25. }
  26. .tabBar(new BottomTabBarStyle($r('sys.media.ohos_app_icon'), "1"))
  27. TabContent() {
  28. Column() {
  29. Text(`barOverlap ${this.barOverlap}`).fontSize(16).margin({ top: this.barOverlap ? '56vp' : 0 })
  30. Text(`barBackgroundColor ${this.barBackgroundColor}`).fontSize(16)
  31. }.width('100%').width('100%').height('100%')
  32. .backgroundColor(Color.Yellow)
  33. }
  34. .tabBar(new BottomTabBarStyle($r('sys.media.ohos_app_icon'), "2"))
  35. TabContent() {
  36. Column() {
  37. Text(`barOverlap ${this.barOverlap}`).fontSize(16).margin({ top: this.barOverlap ? '56vp' : 0 })
  38. Text(`barBackgroundColor ${this.barBackgroundColor}`).fontSize(16)
  39. }.width('100%').width('100%').height('100%')
  40. .backgroundColor(Color.Green)
  41. }
  42. .tabBar(new BottomTabBarStyle($r('sys.media.ohos_app_icon'), "3"))
  43. }
  44. .vertical(false)
  45. .barMode(BarMode.Fixed)
  46. .height('60%')
  47. .barOverlap(this.barOverlap)
  48. .scrollable(true)
  49. .animationDuration(10)
  50. .barBackgroundColor(this.barBackgroundColor)
  51. }
  52. .height(500)
  53. .padding({ top: '24vp', left: '24vp', right: '24vp' })
  54. }
  55. }

tabs5

示例5

本示例通过barGridAlign实现了以栅格化方式设置TabBar的可见区域。

  1. // xxx.ets
  2. @Entry
  3. @Component
  4. struct TabsExample5 {
  5. private controller: TabsController = new TabsController()
  6. @State gridMargin: number = 10
  7. @State gridGutter: number = 10
  8. @State sm: number = -2
  9. @State clickedContent: string = "";
  10. build() {
  11. Column() {
  12. Row() {
  13. Button("gridMargin+10 " + this.gridMargin)
  14. .width('47%')
  15. .height(50)
  16. .margin({ top: 5 })
  17. .onClick((event?: ClickEvent) => {
  18. this.gridMargin += 10
  19. })
  20. .margin({ right: '6%', bottom: '12vp' })
  21. Button("gridMargin-10 " + this.gridMargin)
  22. .width('47%')
  23. .height(50)
  24. .margin({ top: 5 })
  25. .onClick((event?: ClickEvent) => {
  26. this.gridMargin -= 10
  27. })
  28. .margin({ bottom: '12vp' })
  29. }
  30. Row() {
  31. Button("gridGutter+10 " + this.gridGutter)
  32. .width('47%')
  33. .height(50)
  34. .margin({ top: 5 })
  35. .onClick((event?: ClickEvent) => {
  36. this.gridGutter += 10
  37. })
  38. .margin({ right: '6%', bottom: '12vp' })
  39. Button("gridGutter-10 " + this.gridGutter)
  40. .width('47%')
  41. .height(50)
  42. .margin({ top: 5 })
  43. .onClick((event?: ClickEvent) => {
  44. this.gridGutter -= 10
  45. })
  46. .margin({ bottom: '12vp' })
  47. }
  48. Row() {
  49. Button("sm+2 " + this.sm)
  50. .width('47%')
  51. .height(50)
  52. .margin({ top: 5 })
  53. .onClick((event?: ClickEvent) => {
  54. this.sm += 2
  55. })
  56. .margin({ right: '6%' })
  57. Button("sm-2 " + this.sm).width('47%').height(50).margin({ top: 5 })
  58. .onClick((event?: ClickEvent) => {
  59. this.sm -= 2
  60. })
  61. }
  62. Text("点击内容:" + this.clickedContent).width('100%').height(200).margin({ top: 5 })
  63. Tabs({ barPosition: BarPosition.End, controller: this.controller }) {
  64. TabContent() {
  65. Column().width('100%').height('100%').backgroundColor(Color.Pink)
  66. }.tabBar(BottomTabBarStyle.of($r("sys.media.ohos_app_icon"), "1"))
  67. TabContent() {
  68. Column().width('100%').height('100%').backgroundColor(Color.Green)
  69. }.tabBar(BottomTabBarStyle.of($r("sys.media.ohos_app_icon"), "2"))
  70. TabContent() {
  71. Column().width('100%').height('100%').backgroundColor(Color.Blue)
  72. }.tabBar(BottomTabBarStyle.of($r("sys.media.ohos_app_icon"), "3"))
  73. }
  74. .width('350vp')
  75. .animationDuration(300)
  76. .height('60%')
  77. .barGridAlign({ sm: this.sm, margin: this.gridMargin, gutter: this.gridGutter })
  78. .backgroundColor(0xf1f3f5)
  79. .onTabBarClick((index: number) => {
  80. this.clickedContent += "now index " + index + " is clicked\n";
  81. })
  82. }
  83. .width('100%')
  84. .height(500)
  85. .margin({ top: 5 })
  86. .padding('10vp')
  87. }
  88. }

tabs5

示例6

本示例实现了barMode的ScrollableBarModeOptions参数,该参数仅在Scrollable模式下有效。

  1. // xxx.ets
  2. @Entry
  3. @Component
  4. struct TabsExample6 {
  5. private controller: TabsController = new TabsController()
  6. @State scrollMargin: number = 0
  7. @State layoutStyle: LayoutStyle = LayoutStyle.ALWAYS_CENTER
  8. @State text: string = "文本"
  9. build() {
  10. Column() {
  11. Row() {
  12. Button("scrollMargin+10 " + this.scrollMargin)
  13. .width('47%')
  14. .height(50)
  15. .margin({ top: 5 })
  16. .onClick((event?: ClickEvent) => {
  17. this.scrollMargin += 10
  18. })
  19. .margin({ right: '6%', bottom: '12vp' })
  20. Button("scrollMargin-10 " + this.scrollMargin)
  21. .width('47%')
  22. .height(50)
  23. .margin({ top: 5 })
  24. .onClick((event?: ClickEvent) => {
  25. this.scrollMargin -= 10
  26. })
  27. .margin({ bottom: '12vp' })
  28. }
  29. Row() {
  30. Button("文本增加 ")
  31. .width('47%')
  32. .height(50)
  33. .margin({ top: 5 })
  34. .onClick((event?: ClickEvent) => {
  35. this.text += '文本增加'
  36. })
  37. .margin({ right: '6%', bottom: '12vp' })
  38. Button("文本重置")
  39. .width('47%')
  40. .height(50)
  41. .margin({ top: 5 })
  42. .onClick((event?: ClickEvent) => {
  43. this.text = "文本"
  44. })
  45. .margin({ bottom: '12vp' })
  46. }
  47. Row() {
  48. Button("layoutStyle.ALWAYS_CENTER")
  49. .width('100%')
  50. .height(50)
  51. .margin({ top: 5 })
  52. .fontSize(15)
  53. .onClick((event?: ClickEvent) => {
  54. this.layoutStyle = LayoutStyle.ALWAYS_CENTER;
  55. })
  56. .margin({ bottom: '12vp' })
  57. }
  58. Row() {
  59. Button("layoutStyle.ALWAYS_AVERAGE_SPLIT")
  60. .width('100%')
  61. .height(50)
  62. .margin({ top: 5 })
  63. .fontSize(15)
  64. .onClick((event?: ClickEvent) => {
  65. this.layoutStyle = LayoutStyle.ALWAYS_AVERAGE_SPLIT;
  66. })
  67. .margin({ bottom: '12vp' })
  68. }
  69. Row() {
  70. Button("layoutStyle.SPACE_BETWEEN_OR_CENTER")
  71. .width('100%')
  72. .height(50)
  73. .margin({ top: 5 })
  74. .fontSize(15)
  75. .onClick((event?: ClickEvent) => {
  76. this.layoutStyle = LayoutStyle.SPACE_BETWEEN_OR_CENTER;
  77. })
  78. .margin({ bottom: '12vp' })
  79. }
  80. Tabs({ barPosition: BarPosition.End, controller: this.controller }) {
  81. TabContent() {
  82. Column().width('100%').height('100%').backgroundColor(Color.Pink)
  83. }.tabBar(SubTabBarStyle.of(this.text))
  84. TabContent() {
  85. Column().width('100%').height('100%').backgroundColor(Color.Green)
  86. }.tabBar(SubTabBarStyle.of(this.text))
  87. TabContent() {
  88. Column().width('100%').height('100%').backgroundColor(Color.Blue)
  89. }.tabBar(SubTabBarStyle.of(this.text))
  90. }
  91. .animationDuration(300)
  92. .height('60%')
  93. .backgroundColor(0xf1f3f5)
  94. .barMode(BarMode.Scrollable, { margin: this.scrollMargin, nonScrollableLayoutStyle: this.layoutStyle })
  95. }
  96. .width('100%')
  97. .height(500)
  98. .margin({ top: 5 })
  99. .padding('24vp')
  100. }
  101. }

tabs5

示例7

本示例通过customContentTransition实现了自定义Tabs页面的切换动画。

  1. // xxx.ets
  2. @Entry
  3. @Component
  4. struct TabsCustomAnimationExample {
  5. @State useCustomAnimation: boolean = true
  6. @State tabContent0Scale: number = 1.0
  7. @State tabContent1Scale: number = 1.0
  8. @State tabContent0Opacity: number = 1.0
  9. @State tabContent1Opacity: number = 1.0
  10. @State tabContent2Opacity: number = 1.0
  11. tabsController: TabsController = new TabsController()
  12. private firstTimeout: number = 3000
  13. private secondTimeout: number = 5000
  14. private first2secondDuration: number = 3000
  15. private second2thirdDuration: number = 5000
  16. private first2thirdDuration: number = 2000
  17. private baseCustomAnimation: (from: number, to: number) => TabContentAnimatedTransition = (from: number, to: number) => {
  18. if ((from === 0 && to === 1) || (from === 1 && to === 0)) {
  19. // 缩放动画
  20. let firstCustomTransition = {
  21. timeout: this.firstTimeout,
  22. transition: (proxy: TabContentTransitionProxy) => {
  23. if (proxy.from === 0 && proxy.to === 1) {
  24. this.tabContent0Scale = 1.0
  25. this.tabContent1Scale = 0.5
  26. } else {
  27. this.tabContent0Scale = 0.5
  28. this.tabContent1Scale = 1.0
  29. }
  30. animateTo({
  31. duration: this.first2secondDuration,
  32. onFinish: () => {
  33. proxy.finishTransition()
  34. }
  35. }, () => {
  36. if (proxy.from === 0 && proxy.to === 1) {
  37. this.tabContent0Scale = 0.5
  38. this.tabContent1Scale = 1.0
  39. } else {
  40. this.tabContent0Scale = 1.0
  41. this.tabContent1Scale = 0.5
  42. }
  43. })
  44. }
  45. } as TabContentAnimatedTransition;
  46. return firstCustomTransition;
  47. } else {
  48. // 透明度动画
  49. let secondCustomTransition = {
  50. timeout: this.secondTimeout,
  51. transition: (proxy: TabContentTransitionProxy) => {
  52. if ((proxy.from === 1 && proxy.to === 2) || (proxy.from === 2 && proxy.to === 1)) {
  53. if (proxy.from === 1 && proxy.to === 2) {
  54. this.tabContent1Opacity = 1.0
  55. this.tabContent2Opacity = 0.5
  56. } else {
  57. this.tabContent1Opacity = 0.5
  58. this.tabContent2Opacity = 1.0
  59. }
  60. animateTo({
  61. duration: this.second2thirdDuration,
  62. onFinish: () => {
  63. proxy.finishTransition()
  64. }
  65. }, () => {
  66. if (proxy.from === 1 && proxy.to === 2) {
  67. this.tabContent1Opacity = 0.5
  68. this.tabContent2Opacity = 1.0
  69. } else {
  70. this.tabContent1Opacity = 1.0
  71. this.tabContent2Opacity = 0.5
  72. }
  73. })
  74. } else if ((proxy.from === 0 && proxy.to === 2) || (proxy.from === 2 && proxy.to === 0)) {
  75. if (proxy.from === 0 && proxy.to === 2) {
  76. this.tabContent0Opacity = 1.0
  77. this.tabContent2Opacity = 0.5
  78. } else {
  79. this.tabContent0Opacity = 0.5
  80. this.tabContent2Opacity = 1.0
  81. }
  82. animateTo({
  83. duration: this.first2thirdDuration,
  84. onFinish: () => {
  85. proxy.finishTransition()
  86. }
  87. }, () => {
  88. if (proxy.from === 0 && proxy.to === 2) {
  89. this.tabContent0Opacity = 0.5
  90. this.tabContent2Opacity = 1.0
  91. } else {
  92. this.tabContent0Opacity = 1.0
  93. this.tabContent2Opacity = 0.5
  94. }
  95. })
  96. }
  97. }
  98. } as TabContentAnimatedTransition;
  99. return secondCustomTransition;
  100. }
  101. }
  102. build() {
  103. Column() {
  104. Tabs({ controller: this.tabsController }) {
  105. TabContent() {
  106. Text("Red")
  107. }
  108. .tabBar("Red")
  109. .scale({ x: this.tabContent0Scale, y: this.tabContent0Scale })
  110. .backgroundColor(Color.Red)
  111. .opacity(this.tabContent0Opacity)
  112. .width(100)
  113. .height(100)
  114. TabContent() {
  115. Text("Yellow")
  116. }
  117. .tabBar("Yellow")
  118. .scale({ x: this.tabContent1Scale, y: this.tabContent1Scale })
  119. .backgroundColor(Color.Yellow)
  120. .opacity(this.tabContent1Opacity)
  121. .width(200)
  122. .height(200)
  123. TabContent() {
  124. Text("Blue")
  125. }
  126. .tabBar("Blue")
  127. .backgroundColor(Color.Blue)
  128. .opacity(this.tabContent2Opacity)
  129. .width(300)
  130. .height(300)
  131. }
  132. .backgroundColor(0xf1f3f5)
  133. .width('100%')
  134. .height(500)
  135. .margin({ top: 5 })
  136. .customContentTransition(this.useCustomAnimation ? this.baseCustomAnimation : undefined)
  137. .barMode(BarMode.Scrollable)
  138. .onChange((index: number) => {
  139. console.info("onChange index: " + index)
  140. })
  141. .onTabBarClick((index: number) => {
  142. console.info("onTabBarClick index: " + index)
  143. })
  144. }
  145. }
  146. }

tabs5

示例8

本示例通过onChange、onAnimationStart、onAnimationEnd、onGestureSwipe等接口实现了自定义TabBar的切换动画。

  1. // xxx.ets
  2. @Entry
  3. @Component
  4. struct TabsExample {
  5. @State currentIndex: number = 0
  6. @State animationDuration: number = 300
  7. @State indicatorLeftMargin: number = 0
  8. @State indicatorWidth: number = 0
  9. private tabsWidth: number = 0
  10. @Builder
  11. tabBuilder(index: number, name: string) {
  12. Column() {
  13. Text(name)
  14. .fontSize(16)
  15. .fontColor(this.currentIndex === index ? '#007DFF' : '#182431')
  16. .fontWeight(this.currentIndex === index ? 500 : 400)
  17. .id(index.toString())
  18. .onAreaChange((oldValue: Area,newValue: Area) => {
  19. if (this.currentIndex === index && (this.indicatorLeftMargin === 0 || this.indicatorWidth === 0)){
  20. if (newValue.position.x != undefined) {
  21. let positionX = Number.parseFloat(newValue.position.x.toString())
  22. this.indicatorLeftMargin = Number.isNaN(positionX) ? 0 : positionX
  23. }
  24. let width = Number.parseFloat(newValue.width.toString())
  25. this.indicatorWidth = Number.isNaN(width) ? 0 : width
  26. }
  27. })
  28. }.width('100%')
  29. }
  30. build() {
  31. Stack({ alignContent: Alignment.TopStart }) {
  32. Tabs({ barPosition: BarPosition.Start }) {
  33. TabContent() {
  34. Column().width('100%').height('100%').backgroundColor('#00CB87')
  35. }.tabBar(this.tabBuilder(0, 'green'))
  36. TabContent() {
  37. Column().width('100%').height('100%').backgroundColor('#007DFF')
  38. }.tabBar(this.tabBuilder(1, 'blue'))
  39. TabContent() {
  40. Column().width('100%').height('100%').backgroundColor('#FFBF00')
  41. }.tabBar(this.tabBuilder(2, 'yellow'))
  42. TabContent() {
  43. Column().width('100%').height('100%').backgroundColor('#E67C92')
  44. }.tabBar(this.tabBuilder(3, 'pink'))
  45. }
  46. .onAreaChange((oldValue: Area,newValue: Area)=> {
  47. let width = Number.parseFloat(newValue.width.toString())
  48. this.tabsWidth = Number.isNaN(width) ? 0 : width
  49. })
  50. .barWidth('100%')
  51. .barHeight(56)
  52. .width('100%')
  53. .height(296)
  54. .backgroundColor('#F1F3F5')
  55. .animationDuration(this.animationDuration)
  56. .onChange((index: number) => {
  57. this.currentIndex = index // 监听索引index的变化,实现页签内容的切换。
  58. })
  59. .onAnimationStart((index: number, targetIndex: number, event: TabsAnimationEvent) => {
  60. // 切换动画开始时触发该回调。下划线跟着页面一起滑动,同时宽度渐变。
  61. this.currentIndex = targetIndex
  62. let targetIndexInfo = this.getTextInfo(targetIndex)
  63. this.startAnimateTo(this.animationDuration, targetIndexInfo.left, targetIndexInfo.width)
  64. })
  65. .onAnimationEnd((index: number,event: TabsAnimationEvent) => {
  66. // 切换动画结束时触发该回调。下划线动画停止。
  67. let currentIndicatorInfo = this.getCurrentIndicatorInfo(index,event)
  68. this.startAnimateTo(0,currentIndicatorInfo.left,currentIndicatorInfo.width)
  69. })
  70. .onGestureSwipe((index: number,event: TabsAnimationEvent) => {
  71. // 在页面跟手滑动过程中,逐帧触发该回调。
  72. let currentIndicatorInfo = this.getCurrentIndicatorInfo(index,event)
  73. this.currentIndex = currentIndicatorInfo.index
  74. this.indicatorLeftMargin = currentIndicatorInfo.left
  75. this.indicatorWidth = currentIndicatorInfo.width
  76. })
  77. Column()
  78. .height(2)
  79. .width(this.indicatorWidth)
  80. .margin({ left: this.indicatorLeftMargin, top:48})
  81. .backgroundColor('#007DFF')
  82. }.width('100%')
  83. }
  84. private getTextInfo(index: number): Record<string, number> {
  85. let strJson = getInspectorByKey(index.toString())
  86. try {
  87. let obj: Record<string, string> = JSON.parse(strJson)
  88. let rectInfo: number[][] = JSON.parse('[' + obj.$rect + ']')
  89. return { 'left': px2vp(rectInfo[0][0]), 'width': px2vp(rectInfo[1][0] - rectInfo[0][0]) }
  90. } catch (error) {
  91. return { 'left': 0, 'width': 0 }
  92. }
  93. }
  94. private getCurrentIndicatorInfo(index: number, event: TabsAnimationEvent): Record<string, number> {
  95. let nextIndex = index
  96. if (index > 0 && event.currentOffset > 0) {
  97. nextIndex--
  98. } else if (index < 3 && event.currentOffset < 0) {
  99. nextIndex++
  100. }
  101. let indexInfo = this.getTextInfo(index)
  102. let nextIndexInfo = this.getTextInfo(nextIndex)
  103. let swipeRatio = Math.abs(event.currentOffset / this.tabsWidth)
  104. let currentIndex = swipeRatio > 0.5 ? nextIndex : index // 页面滑动超过一半,tabBar切换到下一页。
  105. let currentLeft = indexInfo.left + (nextIndexInfo.left - indexInfo.left) * swipeRatio
  106. let currentWidth = indexInfo.width + (nextIndexInfo.width - indexInfo.width) * swipeRatio
  107. return { 'index': currentIndex, 'left': currentLeft, 'width': currentWidth }
  108. }
  109. private startAnimateTo(duration: number, leftMargin: number, width: number) {
  110. animateTo({
  111. duration: duration, // 动画时长
  112. curve: Curve.Linear, // 动画曲线
  113. iterations: 1, // 播放次数
  114. playMode: PlayMode.Normal, // 动画模式
  115. onFinish: () => {
  116. console.info('play end')
  117. }
  118. }, () => {
  119. this.indicatorLeftMargin = leftMargin
  120. this.indicatorWidth = width
  121. })
  122. }
  123. }

最后,有很多小伙伴不知道学习哪些鸿蒙开发技术?不知道需要重点掌握哪些鸿蒙应用开发知识点?而且学习时频繁踩坑,最终浪费大量时间。所以有一份实用的鸿蒙(HarmonyOS NEXT)资料用来跟着学习是非常有必要的。 

这份鸿蒙(HarmonyOS NEXT)资料包含了鸿蒙开发必掌握的核心知识要点,内容包含了ArkTS、ArkUI开发组件、Stage模型、多端部署、分布式应用开发、音频、视频、WebGL、OpenHarmony多媒体技术、Napi组件、OpenHarmony内核、Harmony南向开发、鸿蒙项目实战等等)鸿蒙(HarmonyOS NEXT)技术知识点。

希望这一份鸿蒙学习资料能够给大家带来帮助,有需要的小伙伴自行领取,限时开源,先到先得~无套路领取!!

获取这份完整版高清学习路线,请点击→纯血版全套鸿蒙HarmonyOS学习资料

鸿蒙(HarmonyOS NEXT)最新学习路线

  •  HarmonOS基础技能

  • HarmonOS就业必备技能 
  •  HarmonOS多媒体技术

  • 鸿蒙NaPi组件进阶

  • HarmonOS高级技能

  • 初识HarmonOS内核 
  • 实战就业级设备开发

有了路线图,怎么能没有学习资料呢,小编也准备了一份联合鸿蒙官方发布笔记整理收纳的一套系统性的鸿蒙(OpenHarmony )学习手册(共计1236页)鸿蒙(OpenHarmony )开发入门教学视频,内容包含:ArkTS、ArkUI、Web开发、应用模型、资源分类…等知识点。

获取以上完整版高清学习路线,请点击→纯血版全套鸿蒙HarmonyOS学习资料

《鸿蒙 (OpenHarmony)开发入门教学视频》

《鸿蒙生态应用开发V2.0白皮书》

图片

《鸿蒙 (OpenHarmony)开发基础到实战手册》

OpenHarmony北向、南向开发环境搭建

图片

 《鸿蒙开发基础》

  • ArkTS语言
  • 安装DevEco Studio
  • 运用你的第一个ArkTS应用
  • ArkUI声明式UI开发
  • .……

图片

 《鸿蒙开发进阶》

  • Stage模型入门
  • 网络管理
  • 数据管理
  • 电话服务
  • 分布式应用开发
  • 通知与窗口管理
  • 多媒体技术
  • 安全技能
  • 任务管理
  • WebGL
  • 国际化开发
  • 应用测试
  • DFX面向未来设计
  • 鸿蒙系统移植和裁剪定制
  • ……

图片

《鸿蒙进阶实战》

  • ArkTS实践
  • UIAbility应用
  • 网络案例
  • ……

图片

 获取以上完整鸿蒙HarmonyOS学习资料,请点击→纯血版全套鸿蒙HarmonyOS学习资料

总结

总的来说,华为鸿蒙不再兼容安卓,对中年程序员来说是一个挑战,也是一个机会。只有积极应对变化,不断学习和提升自己,他们才能在这个变革的时代中立于不败之地。 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/274749
推荐阅读
相关标签
  

闽ICP备14008679号