鸿蒙5.0开发进阶:UI框架-ArkTS组件(TabContent)

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

往期鸿蒙全套实战文章必看:(文中附带全栈鸿蒙学习资料)


TabContent

仅在Tabs中使用,对应一个切换页签的内容视图。

说明

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

子组件

支持单个子组件。

说明

可内置系统组件和自定义组件,支持渲染控制类型(if/else、ForEach和LazyForEach)。

接口

TabContent()

元服务API: 从API version 11开始,该接口支持在元服务中使用。

系统能力: SystemCapability.ArkUI.ArkUI.Full

属性

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

tabBar

tabBar(value: string | Resource | CustomBuilder | { icon?: string | Resource; text?: string | Resource })

设置TabBar上显示内容。

如果icon采用svg格式图源,则要求svg图源删除其自有宽高属性值。如采用带有自有宽高属性的svg图源,icon大小则是svg本身内置的宽高属性值大小。

设置的内容超出tabbar页签时进行裁切。

元服务API: 从API version 11开始,该接口支持在元服务中使用。

系统能力: SystemCapability.ArkUI.ArkUI.Full

参数:

参数名类型必填说明
value

string | Resource |

CustomBuilder8+| {

icon?: string | Resource,

text?: string | Resource

}

是

TabBar上显示内容。

CustomBuilder: 构造器,内部可以传入组件(API8版本以上适用)。

tabBar9+

tabBar(value: SubTabBarStyle | BottomTabBarStyle)

设置TabBar上显示内容。底部样式没有下划线效果。icon异常时显示灰色图块。

元服务API: 从API version 11开始,该接口支持在元服务中使用。

系统能力: SystemCapability.ArkUI.ArkUI.Full

参数:

参数名类型必填说明
valueSubTabBarStyle | BottomTabBarStyle是

TabBar上显示内容。

SubTabBarStyle: 子页签样式。

BottomTabBarStyle: 底部页签和侧边页签样式。

说明

  • TabContent组件不支持设置通用宽度属性,其宽度默认撑满Tabs父组件。
  • TabContent组件不支持设置通用高度属性,其高度由Tabs父组件高度与TabBar组件高度决定。
  • vertical属性为false值,交换上述2个限制。
  • TabContent组件不支持内容过长时页面的滑动,如需页面滑动,可嵌套List使用。
  • 若TabContent内部有可获焦组件,Tabs组件内TabContent组件和TabBar组件之间的走焦,仅支持使用键盘上下左右控制。

SubTabBarStyle9+对象说明

子页签样式。打开后在切换页签时会播放跳转动画。

constructor

constructor(content: ResourceStr)

SubTabBarStyle的构造函数。

元服务API: 从API version 11开始,该接口支持在元服务中使用。

系统能力: SystemCapability.ArkUI.ArkUI.Full

参数:

参数名类型必填说明
contentResourceStr是页签内的文字内容。

constructor12+

constructor(content: ResourceStr | ComponentContent)

SubTabBarStyle的构造函数。支持ComponentContent设置自定义内容。

元服务API: 从API version 12开始,该接口支持在元服务中使用。

系统能力: SystemCapability.ArkUI.ArkUI.Full

参数:

参数名类型必填说明
contentResourceStr | ComponentContent是

页签内的内容。

说明:

1.自定义内容不支持labelStyle属性。

2.自定义内容超出页签范围,则不显示超出部分。

3.自定义内容小于页签范围,则会居中对齐。

4.自定义内容异常或无可用显示组件,则显示空白。

of10+

static of(content: ResourceStr): SubTabBarStyle

SubTabBarStyle的静态构造函数。

元服务API: 从API version 11开始,该接口支持在元服务中使用。

系统能力: SystemCapability.ArkUI.ArkUI.Full

参数:

参数名类型必填说明
contentResourceStr是页签内的文字内容。

返回值:

类型说明
SubTabBarStyle返回创建的SubTabBarStyle对象。

of12+

static of(content: ResourceStr | ComponentContent): SubTabBarStyle

SubTabBarStyle的静态构造函数。

元服务API: 从API version 12开始,该接口支持在元服务中使用。

系统能力: SystemCapability.ArkUI.ArkUI.Full

参数:

参数名类型必填说明
contentResourceStr | ComponentContent是

页签内的内容。支持ComponentContent设置自定义内容。

说明:

1.自定义内容不支持labelStyle属性。

2.自定义内容超出页签范围,则不显示超出部分。

3.自定义内容小于页签范围,则会居中对齐。

4.自定义内容异常或无可用显示组件,则显示空白。

返回值:

类型说明
SubTabBarStyle返回创建的SubTabBarStyle对象。

indicator10+

indicator(value: IndicatorStyle): SubTabBarStyle

设置选中子页签的下划线风格。子页签的下划线风格仅在水平模式下有效。

元服务API: 从API version 11开始,该接口支持在元服务中使用。

系统能力: SystemCapability.ArkUI.ArkUI.Full

参数:

参数名类型必填说明
valueIndicatorStyle是选中子页签的下划线风格对象。

返回值:

类型说明
SubTabBarStyle返回SubTabBarStyle对象本身。

selectedMode10+

selectedMode(value: SelectedMode): SubTabBarStyle

设置选中子页签的显示方式。子页签的显示方式仅在水平模式下有效。

元服务API: 从API version 11开始,该接口支持在元服务中使用。

系统能力: SystemCapability.ArkUI.ArkUI.Full

参数:

参数名类型必填说明
valueSelectedMode是

选中子页签的显示方式。

默认值:SelectedMode.INDICATOR

返回值:

类型说明
SubTabBarStyle返回SubTabBarStyle对象本身。

board10+

board(value: BoardStyle): SubTabBarStyle

设置选中子页签的背板风格。子页签的背板风格仅在水平模式下有效。

元服务API: 从API version 11开始,该接口支持在元服务中使用。

系统能力: SystemCapability.ArkUI.ArkUI.Full

参数:

参数名类型必填说明
valueBoardStyle是选中子页签的背板风格对象。

返回值:

类型说明
SubTabBarStyle返回SubTabBarStyle对象本身。

labelStyle10+

labelStyle(value: LabelStyle): SubTabBarStyle

设置子页签的label文本和字体的样式。

元服务API: 从API version 11开始,该接口支持在元服务中使用。

系统能力: SystemCapability.ArkUI.ArkUI.Full

参数:

参数名类型必填说明
valueLabelStyle是子页签的label文本和字体的样式对象。

返回值:

类型说明
SubTabBarStyle返回SubTabBarStyle对象本身。

padding10+

padding(value: Padding | Dimension): SubTabBarStyle

设置子页签的内边距属性(不支持百分比设置)。使用Dimension时,四个方向内边距同时生效。

元服务API: 从API version 11开始,该接口支持在元服务中使用。

系统能力: SystemCapability.ArkUI.ArkUI.Full

参数:

参数名类型必填说明
valuePadding | Dimension是

子页签的内边距属性。

默认值:{left:8.0vp,right:8.0vp,top:17.0vp,bottom:18.0vp}

返回值:

类型说明
SubTabBarStyle返回SubTabBarStyle对象本身。

padding12+

padding(padding: LocalizedPadding): SubTabBarStyle

设置子页签的内边距属性,支持镜像能力(不支持百分比设置)。

元服务API: 从API version 12开始,该接口支持在元服务中使用。

系统能力: SystemCapability.ArkUI.ArkUI.Full

参数:

参数名类型必填说明
paddingLocalizedPadding是

子页签的内边距属性。

默认值:{start:LengthMetircs.vp(8),end:LengthMetircs.vp(8),

top:LengthMetircs.vp(17),bottom:LengthMetircs.vp(18)}

返回值:

类型说明
SubTabBarStyle返回SubTabBarStyle对象本身。

id11+

id(value: string): SubTabBarStyle

设置子页签的id。

元服务API: 从API version 12开始,该接口支持在元服务中使用。

系统能力: SystemCapability.ArkUI.ArkUI.Full

参数:

参数名类型必填说明
valuestring是子页签的id。

返回值:

类型说明
SubTabBarStyle返回SubTabBarStyle对象本身。

IndicatorStyle10+对象说明

元服务API: 从API version 11开始,该接口支持在元服务中使用。

系统能力: SystemCapability.ArkUI.ArkUI.Full

名称类型必填说明
colorResourceColor否

下划线的颜色和背板颜色。

默认值:#FF007DFF

heightLength否

下划线的高度(不支持百分比设置)。

默认值:2.0

单位:vp

widthLength否

下划线的宽度(不支持百分比设置)。

默认值:0.0

单位:vp

说明:

宽度设置为0时,按页签文本宽度显示。

borderRadiusLength否

下划线的圆角半径(不支持百分比设置)。

默认值:0.0

单位:vp

marginTopLength否

下划线与文字的间距(不支持百分比设置)。

默认值:8.0

单位:vp

SelectedMode10+枚举说明

元服务API: 从API version 11开始,该接口支持在元服务中使用。

系统能力: SystemCapability.ArkUI.ArkUI.Full

名称描述
INDICATOR使用下划线模式。
BOARD使用背板模式。

BoardStyle10+对象说明

元服务API: 从API version 11开始,该接口支持在元服务中使用。

系统能力: SystemCapability.ArkUI.ArkUI.Full

名称类型必填说明
borderRadiusLength否

背板的圆角半径(不支持百分比设置)。

默认值:8.0

单位:vp

LabelStyle10+对象说明

系统能力: SystemCapability.ArkUI.ArkUI.Full

名称类型必填说明
overflowTextOverflow否

设置Label文本超长时的显示方式。默认值是省略号截断。

元服务API: 从API version 11开始,该接口支持在元服务中使用。

maxLinesnumber否

设置Label文本的最大行数。如果指定此参数,则文本最多不会超过指定的行。如果有多余的文本,可以通过textOverflow来指定截断方式。默认值是1。

元服务API: 从API version 11开始,该接口支持在元服务中使用。

minFontSizenumber | ResourceStr否

设置Label文本最小显示字号(不支持百分比设置)。需配合maxFontSize以及maxLines或布局大小限制使用。自适应文本大小生效后,font.size不生效。默认值是0.0fp。

元服务API: 从API version 11开始,该接口支持在元服务中使用。

maxFontSizenumber | ResourceStr否

设置Label文本最大显示字号(不支持百分比设置)。需配合minFontSize以及maxLines或布局大小限制使用。自适应文本大小生效后,font.size不生效。默认值是0.0fp。

元服务API: 从API version 11开始,该接口支持在元服务中使用。

heightAdaptivePolicyTextHeightAdaptivePolicy否

设置Label文本自适应高度的方式。默认值是最大行数优先。

元服务API: 从API version 11开始,该接口支持在元服务中使用。

fontFont否

设置Label文本字体样式。

当页签为子页签时,默认值是字体大小16.0fp、字体类型'HarmonyOS Sans',字体风格正常,字重正常。

当页签为底部页签时,默认值是字体大小10.0fp、字体类型'HarmonyOS Sans',字体风格正常,字重中等。

从API version 12开始,底部页签页签内容左右排布时默认字体大小为12.0fp。

元服务API: 从API version 11开始,该接口支持在元服务中使用。

unselectedColor12+ResourceColor否

设置Label文本字体未选中时的颜色。

默认值:#99182431

元服务API: 从API version 12开始,该接口支持在元服务中使用。

selectedColor12+ResourceColor否

设置Label文本字体选中时的颜色。

默认值:#FF007DFF

元服务API: 从API version 12开始,该接口支持在元服务中使用。

BottomTabBarStyle9+对象说明

底部页签和侧边页签样式。

constructor

constructor(icon: ResourceStr | TabBarSymbol, text: ResourceStr)

BottomTabBarStyle的构造函数。

元服务API: 从API version 11开始,该接口支持在元服务中使用。

系统能力: SystemCapability.ArkUI.ArkUI.Full

参数:

参数名类型必填说明
iconResourceStr | TabBarSymbol12+是页签内的图片内容。
textResourceStr是页签内的文字内容。

of10+

static of(icon: ResourceStr | TabBarSymbol, text: ResourceStr): BottomTabBarStyle

BottomTabBarStyle的静态构造函数。

元服务API: 从API version 11开始,该接口支持在元服务中使用。

系统能力: SystemCapability.ArkUI.ArkUI.Full

参数:

参数名类型必填说明
iconResourceStr | TabBarSymbol12+是页签内的图片内容。
textResourceStr是页签内的文字内容。

返回值:

类型说明
BottomTabBarStyle返回创建的BottomTabBarStyle对象。

padding10+

padding(value: Padding | Dimension | LocalizedPadding): BottomTabBarStyle

设置底部页签的内边距属性(不支持百分比设置)。使用Dimension时,四个方向内边距同时生效。

元服务API: 从API version 11开始,该接口支持在元服务中使用。

系统能力: SystemCapability.ArkUI.ArkUI.Full

参数:

参数名类型必填说明
valuePadding | Dimension | LocalizedPadding12+是

底部页签的内边距。

默认值:{left:4.0vp,right:4.0vp,top:0.0vp,bottom:0.0vp}

使用LocalizedPadding时,支持镜像能力。

默认值:{start:LengthMetircs.vp(4),end:LengthMetircs.vp(4),

top:LengthMetircs.vp(0),bottom:LengthMetircs.vp(0)}

返回值:

类型说明
BottomTabBarStyle返回BottomTabBarStyle对象本身。

verticalAlign10+

verticalAlign(value: VerticalAlign): BottomTabBarStyle

设置底部页签的图片、文字在垂直方向上的对齐格式。

元服务API: 从API version 11开始,该接口支持在元服务中使用。

系统能力: SystemCapability.ArkUI.ArkUI.Full

参数:

参数名类型必填说明
valueVerticalAlign是

底部页签的图片、文字在垂直方向上的对齐格式。

默认值:VerticalAlign.Center

返回值:

类型说明
BottomTabBarStyle返回BottomTabBarStyle对象本身。

layoutMode10+

layoutMode(value: LayoutMode): BottomTabBarStyle

设置底部页签的图片、文字排布的方式。

元服务API: 从API version 11开始,该接口支持在元服务中使用。

系统能力: SystemCapability.ArkUI.ArkUI.Full

参数:

参数名类型必填说明
valueLayoutMode是

底部页签的图片、文字排布的方式,具体参照LayoutMode枚举。

默认值:LayoutMode.VERTICAL

返回值:

类型说明
BottomTabBarStyle返回BottomTabBarStyle对象本身。

symmetricExtensible10+

symmetricExtensible(value: boolean): BottomTabBarStyle

设置底部页签的图片、文字是否可以对称借左右底部页签的空余位置中的最小值,仅fixed水平模式下在底部页签之间有效。

元服务API: 从API version 11开始,该接口支持在元服务中使用。

系统能力: SystemCapability.ArkUI.ArkUI.Full

参数:

参数名类型必填说明
valueboolean是

底部页签的图片、文字是否可以对称借左右底部页签的空余位置中的最小值。

默认值:false

返回值:

类型说明
BottomTabBarStyle返回BottomTabBarStyle对象本身。

labelStyle10+

labelStyle(value: LabelStyle): BottomTabBarStyle

设置底部页签的label文本和字体的样式。

元服务API: 从API version 11开始,该接口支持在元服务中使用。

系统能力: SystemCapability.ArkUI.ArkUI.Full

参数:

参数名类型必填说明
valueLabelStyle是底部页签的label文本和字体的样式。

返回值:

类型说明
BottomTabBarStyle返回BottomTabBarStyle对象本身。

id11+

id(value: string): BottomTabBarStyle

设置底部页签的id。

元服务API: 从API version 12开始,该接口支持在元服务中使用。

系统能力: SystemCapability.ArkUI.ArkUI.Full

参数:

参数名类型必填说明
valuestring是设置底部页签的id。

返回值:

类型说明
BottomTabBarStyle返回BottomTabBarStyle对象本身。

iconStyle12+

iconStyle(style: TabBarIconStyle): BottomTabBarStyle

置底部页签的label图标的样式。

元服务API: 从API version 12开始,该接口支持在元服务中使用。

系统能力: SystemCapability.ArkUI.ArkUI.Full

参数:

参数名类型必填说明
styleTabBarIconStyle是底部页签的label图标的样式。

返回值:

类型说明
BottomTabBarStyle返回BottomTabBarStyle对象本身。

TabBarSymbol12+对象说明

元服务API: 从API version 12开始,该接口支持在元服务中使用。

系统能力: SystemCapability.ArkUI.ArkUI.Full

名称类型必填说明
normalSymbolGlyphModifier是

页签内symbol图标普通态样式。

默认值:fontColor:#66182431,renderingStrategy:SymbolRenderingStrategy.MULTIPLE_OPACITY,fontSize:24vp

selectedSymbolGlyphModifier否

页签内symbol图标选中态样式。

默认值:fontColor:#ff007dff,renderingStrategy:SymbolRenderingStrategy.MULTIPLE_OPACITY,fontSize:24vp

LayoutMode10+枚举说明

元服务API: 从API version 11开始,该接口支持在元服务中使用。

系统能力: SystemCapability.ArkUI.ArkUI.Full

名称描述
AUTO若页签宽度大于104vp,页签内容为左右排布,否则页签内容为上下排布。仅TabBar为垂直模式或Fixed水平模式时有效。
VERTICAL页签内容上下排布。
HORIZONTAL页签内容左右排布。

TabBarIconStyle12+对象说明

元服务API: 从API version 12开始,该接口支持在元服务中使用。

系统能力: SystemCapability.ArkUI.ArkUI.Full

名称类型必填说明
unselectedColorResourceColor否

设置Label图标未选中时的颜色。

默认值:#33182431

说明:

仅对svg图源生效,设置后会替换svg图片的填充颜色。

selectedColorResourceColor否

设置Label图标选中时的颜色。

默认值:#FF007DFF

说明:

仅对svg图源生效,设置后会替换svg图片的填充颜色。

事件

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

onWillShow12+

onWillShow(event: VoidCallback)

逻辑回调,TabContent将要显示的时候触发该回调。场景包括TabContent首次显示,TabContent切换,页面切换,窗口前后台切换。

元服务API: 从API version 12开始,该接口支持在元服务中使用。

系统能力: SystemCapability.ArkUI.ArkUI.Full

参数:

参数名类型必填说明
eventVoidCallback是TabContent将要显示的回调函数。

onWillHide12+

onWillHide(event: VoidCallback)

逻辑回调,TabContent将要隐藏的时候触发该回调。场景包括TabContent切换,页面切换,窗口前后台切换。

元服务API: 从API version 12开始,该接口支持在元服务中使用。

系统能力: SystemCapability.ArkUI.ArkUI.Full

参数:

参数名类型必填说明
eventVoidCallback是TabContent将要隐藏的回调函数。

示例

示例1(自定义页签切换联动)

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

// xxx.ets
@Entry
@Component
struct TabContentExample {
  @State fontColor: string = '#182431'
  @State selectedFontColor: string = '#007DFF'
  @State currentIndex: number = 0
  @State selectedIndex: number = 0
  private controller: TabsController = new TabsController()

  @Builder tabBuilder(index: number) {
    Column() {
      Image(this.selectedIndex === index ? '/common/public_icon_on.svg' : '/common/public_icon_off.svg')
        .width(24)
        .height(24)
        .margin({ bottom: 4 })
        .objectFit(ImageFit.Contain)
      Text(`Tab${index + 1}`)
        .fontColor(this.selectedIndex === index ? this.selectedFontColor : this.fontColor)
        .fontSize(10)
        .fontWeight(500)
        .lineHeight(14)
    }.width('100%')
  }

  build() {
    Column() {
      Tabs({ barPosition: BarPosition.End, controller: this.controller }) {
        TabContent() {
          Column() {
            Text('Tab1')
              .fontSize(36)
              .fontColor('#182431')
              .fontWeight(500)
              .opacity(0.4)
              .margin({ top: 30, bottom: 56.5 })
            Divider()
              .strokeWidth(0.5)
              .color('#182431')
              .opacity(0.05)
          }.width('100%')
        }.tabBar(this.tabBuilder(0))

        TabContent() {
          Column() {
            Text('Tab2')
              .fontSize(36)
              .fontColor('#182431')
              .fontWeight(500)
              .opacity(0.4)
              .margin({ top: 30, bottom: 56.5 })
            Divider()
              .strokeWidth(0.5)
              .color('#182431')
              .opacity(0.05)
          }.width('100%')
        }.tabBar(this.tabBuilder(1))

        TabContent() {
          Column() {
            Text('Tab3')
              .fontSize(36)
              .fontColor('#182431')
              .fontWeight(500)
              .opacity(0.4)
              .margin({ top: 30, bottom: 56.5 })
            Divider()
              .strokeWidth(0.5)
              .color('#182431')
              .opacity(0.05)
          }.width('100%')
        }.tabBar(this.tabBuilder(2))

        TabContent() {
          Column() {
            Text('Tab4')
              .fontSize(36)
              .fontColor('#182431')
              .fontWeight(500)
              .opacity(0.4)
              .margin({ top: 30, bottom: 56.5 })
            Divider()
              .strokeWidth(0.5)
              .color('#182431')
              .opacity(0.05)
          }.width('100%')
        }.tabBar(this.tabBuilder(3))
      }
      .vertical(false)
      .barHeight(56)
      .onChange((index: number) => {
        // currentIndex控制TabContent显示页签
        this.currentIndex = index
        this.selectedIndex = index
      })
      .onAnimationStart((index: number, targetIndex: number, event: TabsAnimationEvent) => {
        if (index === targetIndex) {
          return
        }
        // selectedIndex控制自定义TabBar内Image和Text颜色切换
        this.selectedIndex = targetIndex
      })
      .width(360)
      .height(190)
      .backgroundColor('#F1F3F5')
      .margin({ top: 38 })
    }.width('100%')
  }
}

示例2(自定义侧边页签)

本示例通过vertical、barPosition实现侧边页签。

// xxx.ets
@Entry
@Component
struct TabContentExample {
  @State fontColor: string = '#182431'
  @State selectedFontColor: string = '#007DFF'
  @State currentIndex: number = 0
  @State selectedIndex: number = 0
  private controller: TabsController = new TabsController()

  @Builder tabBuilder(index: number) {
    Column() {
      Image(this.selectedIndex === index ? '/common/public_icon_on.svg' : '/common/public_icon_off.svg')
        .width(24)
        .height(24)
        .margin({ bottom: 4 })
        .objectFit(ImageFit.Contain)
      Text('Tab')
        .fontColor(this.selectedIndex === index ? this.selectedFontColor : this.fontColor)
        .fontSize(10)
        .fontWeight(500)
        .lineHeight(14)
    }.width('100%').height('100%').justifyContent(FlexAlign.Center)
  }

  build() {
    Column() {
      Tabs({ barPosition: BarPosition.Start, controller: this.controller }) {
        TabContent()
          .tabBar(this.tabBuilder(0))
        TabContent()
          .tabBar(this.tabBuilder(1))
        TabContent()
          .tabBar(this.tabBuilder(2))
        TabContent()
          .tabBar(this.tabBuilder(3))
      }
      .vertical(true)
      .barWidth(96)
      .barHeight(414)
      .onChange((index: number) => {
        // currentIndex控制TabContent显示页签
        this.currentIndex = index
        this.selectedIndex = index
      })
      .onAnimationStart((index: number, targetIndex: number, event: TabsAnimationEvent) => {
        if (index === targetIndex) {
          return
        }
        // selectedIndex控制自定义TabBar内Image和Text颜色切换
        this.selectedIndex = targetIndex
      })
      .width(96)
      .height(414)
      .backgroundColor('#F1F3F5')
      .margin({ top: 52 })
    }.width('100%')
  }
}

示例3(子页签/底部页签/侧边页签样式对比)

本示例使用了SubTabBarStyle、BottomTabBarStyle实现了子页签、底部页签和侧边页签。

// xxx.ets
@Entry
@Component
struct TabBarStyleExample {
  build() {
    Column({ space: 5 }) {
      Text("子页签样式")
      Column() {
        Tabs({ barPosition: BarPosition.Start }) {
          TabContent() {
            Column().width('100%').height('100%').backgroundColor(Color.Pink)
          }.tabBar(new SubTabBarStyle('Pink'))
          .onWillShow(() => {
            console.info("Pink will show")
          })
          .onWillHide(() => {
            console.info("Pink will hide")
          })

          TabContent() {
            Column().width('100%').height('100%').backgroundColor(Color.Yellow)
          }.tabBar(new SubTabBarStyle('Yellow'))
          .onWillShow(() => {
            console.info("Yellow will show")
          })
          .onWillHide(() => {
            console.info("Yellow will hide")
          })

          TabContent() {
            Column().width('100%').height('100%').backgroundColor(Color.Blue)
          }.tabBar(new SubTabBarStyle('Blue'))
          .onWillShow(() => {
            console.info("Blue will show")
          })
          .onWillHide(() => {
            console.info("Blue will hide")
          })

          TabContent() {
            Column().width('100%').height('100%').backgroundColor(Color.Green)
          }.tabBar(new SubTabBarStyle('Green'))
          .onWillShow(() => {
            console.info("Green will show")
          })
          .onWillHide(() => {
            console.info("Green will hide")
          })
        }
        .vertical(false)
        .scrollable(true)
        .barMode(BarMode.Fixed)
        .onChange((index: number) => {
          console.info(index.toString())
        })
        .width('100%')
        .backgroundColor(0xF1F3F5)
      }.width('100%').height(200)
      Text("底部页签样式")
      Column() {
        Tabs({ barPosition: BarPosition.End }) {
          TabContent() {
            Column().width('100%').height('100%').backgroundColor(Color.Pink)
          }.tabBar(new BottomTabBarStyle($r('sys.media.ohos_app_icon'), 'Pink'))
          .onWillShow(() => {
            console.info("Pink will show")
          })
          .onWillHide(() => {
            console.info("Pink will hide")
          })

          TabContent() {
            Column().width('100%').height('100%').backgroundColor(Color.Yellow)
          }.tabBar(new BottomTabBarStyle($r('sys.media.ohos_app_icon'), 'Yellow'))
          .onWillShow(() => {
            console.info("Yellow will show")
          })
          .onWillHide(() => {
            console.info("Yellow will hide")
          })

          TabContent() {
            Column().width('100%').height('100%').backgroundColor(Color.Blue)
          }.tabBar(new BottomTabBarStyle($r('sys.media.ohos_app_icon'), 'Blue'))
          .onWillShow(() => {
            console.info("Blue will show")
          })
          .onWillHide(() => {
            console.info("Blue will hide")
          })

          TabContent() {
            Column().width('100%').height('100%').backgroundColor(Color.Green)
          }.tabBar(new BottomTabBarStyle($r('sys.media.ohos_app_icon'), 'Green'))
          .onWillShow(() => {
            console.info("Green will show")
          })
          .onWillHide(() => {
            console.info("Green will hide")
          })
        }
        .vertical(false)
        .scrollable(true)
        .barMode(BarMode.Fixed)
        .onChange((index: number) => {
          console.info(index.toString())
        })
        .width('100%')
        .backgroundColor(0xF1F3F5)
      }.width('100%').height(200)
      Text("侧边页签样式")
      Column() {
        Tabs({ barPosition: BarPosition.Start }) {
          TabContent() {
            Column().width('100%').height('100%').backgroundColor(Color.Pink)
          }.tabBar(new BottomTabBarStyle($r('sys.media.ohos_app_icon'), 'Pink'))
          .onWillShow(() => {
            console.info("Pink will show")
          })
          .onWillHide(() => {
            console.info("Pink will hide")
          })

          TabContent() {
            Column().width('100%').height('100%').backgroundColor(Color.Yellow)
          }.tabBar(new BottomTabBarStyle($r('sys.media.ohos_app_icon'), 'Yellow'))
          .onWillShow(() => {
            console.info("Yellow will show")
          })
          .onWillHide(() => {
            console.info("Yellow will hide")
          })

          TabContent() {
            Column().width('100%').height('100%').backgroundColor(Color.Blue)
          }.tabBar(new BottomTabBarStyle($r('sys.media.ohos_app_icon'), 'Blue'))
          .onWillShow(() => {
            console.info("Blue will show")
          })
          .onWillHide(() => {
            console.info("Blue will hide")
          })

          TabContent() {
            Column().width('100%').height('100%').backgroundColor(Color.Green)
          }.tabBar(new BottomTabBarStyle($r('sys.media.ohos_app_icon'), 'Green'))
          .onWillShow(() => {
            console.info("Green will show")
          })
          .onWillHide(() => {
            console.info("Green will hide")
          })
        }
        .vertical(true).scrollable(true).barMode(BarMode.Fixed)
        .onChange((index: number) => {
          console.info(index.toString())
        })
        .width('100%')
        .backgroundColor(0xF1F3F5)
      }.width('100%').height(400)
    }
  }
}

示例4(设置子页签下划线基本属性)

本示例通过SubTabBarStyle中的indicator属性,实现了子页签下划线基本属性的展示。

// xxx.ets
@Entry
@Component
struct TabsAttr {
  private controller: TabsController = new TabsController()
  @State indicatorColor: Color = Color.Blue;
  @State indicatorWidth: number = 40;
  @State indicatorHeight: number = 10;
  @State indicatorBorderRadius: number = 5;
  @State indicatorSpace: number = 10;
  @State subTabBorderRadius: number = 20;
  @State selectedMode: SelectedMode = SelectedMode.INDICATOR;
  private colorFlag: boolean = true;
  private widthFlag: boolean = true;
  private heightFlag: boolean = true;
  private borderFlag: boolean = true;
  private spaceFlag: boolean = true;

  build() {
    Column() {
      Button("下划线颜色变化").width('100%').margin({ bottom: '12vp' })
        .onClick((event?: ClickEvent) => {
          // 对Button组件的宽高属性进行动画配置
          if (this.colorFlag) {
            animateTo({
              duration: 1000, // 动画时长
              curve: Curve.Linear, // 动画曲线
              delay: 200, // 动画延迟
              iterations: 1, // 播放次数
              playMode: PlayMode.Normal, // 动画模式
              onFinish: () => {
                console.info('play end')
              }
            }, () => {
              this.indicatorColor = Color.Red
            })
          } else {
            animateTo({
              duration: 1000, // 动画时长
              curve: Curve.Linear, // 动画曲线
              delay: 200, // 动画延迟
              iterations: 1, // 播放次数
              playMode: PlayMode.Normal, // 动画模式
              onFinish: () => {
                console.info('play end')
              }
            }, () => {
              this.indicatorColor = Color.Yellow
            })
          }
          this.colorFlag = !this.colorFlag
        })
      Button("下划线高度变化").width('100%').margin({ bottom: '12vp' })
        .onClick((event?: ClickEvent) => {
          // 对Button组件的宽高属性进行动画配置
          if (this.heightFlag) {
            animateTo({
              duration: 1000, // 动画时长
              curve: Curve.Linear, // 动画曲线
              delay: 200, // 动画延迟
              iterations: 1, // 播放次数
              playMode: PlayMode.Normal, // 动画模式
              onFinish: () => {
                console.info('play end')
              }
            }, () => {
              this.indicatorHeight = 20
            })
          } else {
            animateTo({
              duration: 1000, // 动画时长
              curve: Curve.Linear, // 动画曲线
              delay: 200, // 动画延迟
              iterations: 1, // 播放次数
              playMode: PlayMode.Normal, // 动画模式
              onFinish: () => {
                console.info('play end')
              }
            }, () => {
              this.indicatorHeight = 10
            })
          }
          this.heightFlag = !this.heightFlag
        })
      Button("下划线宽度变化").width('100%').margin({ bottom: '12vp' })
        .onClick((event?: ClickEvent) => {
          // 对Button组件的宽高属性进行动画配置
          if (this.widthFlag) {
            animateTo({
              duration: 1000, // 动画时长
              curve: Curve.Linear, // 动画曲线
              delay: 200, // 动画延迟
              iterations: 1, // 播放次数
              playMode: PlayMode.Normal, // 动画模式
              onFinish: () => {
                console.info('play end')
              }
            }, () => {
              this.indicatorWidth = 30
            })
          } else {
            animateTo({
              duration: 1000, // 动画时长
              curve: Curve.Linear, // 动画曲线
              delay: 200, // 动画延迟
              iterations: 1, // 播放次数
              playMode: PlayMode.Normal, // 动画模式
              onFinish: () => {
                console.info('play end')
              }
            }, () => {
              this.indicatorWidth = 50
            })
          }
          this.widthFlag = !this.widthFlag
        })
      Button("下划线圆角半径变化").width('100%').margin({ bottom: '12vp' })
        .onClick((event?: ClickEvent) => {
          // 对Button组件的宽高属性进行动画配置
          if (this.borderFlag) {
            animateTo({
              duration: 1000, // 动画时长
              curve: Curve.Linear, // 动画曲线
              delay: 200, // 动画延迟
              iterations: 1, // 播放次数
              playMode: PlayMode.Normal, // 动画模式
              onFinish: () => {
                console.info('play end')
              }
            }, () => {
              this.indicatorBorderRadius = 0
            })
          } else {
            animateTo({
              duration: 1000, // 动画时长
              curve: Curve.Linear, // 动画曲线
              delay: 200, // 动画延迟
              iterations: 1, // 播放次数
              playMode: PlayMode.Normal, // 动画模式
              onFinish: () => {
                console.info('play end')
              }
            }, () => {
              this.indicatorBorderRadius = 5
            })
          }
          this.borderFlag = !this.borderFlag
        })
      Button("下划线间距变化").width('100%').margin({ bottom: '12vp' })
        .onClick((event?: ClickEvent) => {
          // 对Button组件的宽高属性进行动画配置
          if (this.spaceFlag) {
            animateTo({
              duration: 1000, // 动画时长
              curve: Curve.Linear, // 动画曲线
              delay: 200, // 动画延迟
              iterations: 1, // 播放次数
              playMode: PlayMode.Normal, // 动画模式
              onFinish: () => {
                console.info('play end')
              }
            }, () => {
              this.indicatorSpace = 20
            })
          } else {
            animateTo({
              duration: 1000, // 动画时长
              curve: Curve.Linear, // 动画曲线
              delay: 200, // 动画延迟
              iterations: 1, // 播放次数
              playMode: PlayMode.Normal, // 动画模式
              onFinish: () => {
                console.info('play end')
              }
            }, () => {
              this.indicatorSpace = 10
            })
          }
          this.spaceFlag = !this.spaceFlag
        })
      Tabs({ barPosition: BarPosition.End, controller: this.controller }) {
        TabContent() {
          Column().width('100%').height('100%').backgroundColor(Color.Pink).borderRadius('12vp')
        }.tabBar(SubTabBarStyle.of('pink')
          .indicator({
            color: this.indicatorColor, //下划线颜色
            height: this.indicatorHeight, //下划线高度
            width: this.indicatorWidth, //下划线宽度
            borderRadius: this.indicatorBorderRadius, //下划线圆角半径
            marginTop: this.indicatorSpace //下划线与文字间距
          })
          .selectedMode(this.selectedMode)
          .board({ borderRadius: this.subTabBorderRadius })
          .labelStyle({})
        )

        TabContent() {
          Column().width('100%').height('100%').backgroundColor(Color.Yellow).borderRadius('12vp')
        }.tabBar('yellow')

        TabContent() {
          Column().width('100%').height('100%').backgroundColor(Color.Blue).borderRadius('12vp')
        }.tabBar('blue')

        TabContent() {
          Column().width('100%').height('100%').backgroundColor(Color.Green).borderRadius('12vp')
        }.tabBar('green')

        TabContent() {
          Column().width('100%').height('100%').backgroundColor(Color.Gray).borderRadius('12vp')
        }.tabBar('gray')

        TabContent() {
          Column().width('100%').height('100%').backgroundColor(Color.Orange).borderRadius('12vp')
        }.tabBar('orange')
      }
      .vertical(false)
      .scrollable(true)
      .barMode(BarMode.Scrollable)
      .barHeight(140)
      .animationDuration(400)
      .onChange((index: number) => {
        console.info(index.toString())
      })
      .backgroundColor(0xF5F5F5)
      .height(320)
    }.width('100%').height(250).padding({ top: '24vp', left: '24vp', right: '24vp' })
  }
}

示例5(设置子页签文本自适应高度属性)

本示例通过heightAdaptivePolicy实现了子页签文本高度自适应。

// xxx.ets
@Entry
@Component
struct TabsTextOverflow {
  @State message: string = 'Hello World'
  private controller: TabsController = new TabsController()
  @State subTabOverflowOpaque: boolean = true;

  build() {
    Column() {
      Tabs({ barPosition: BarPosition.Start, controller: this.controller }) {
        TabContent() {
          Column() {
            Text('单行省略号截断').fontSize(30).fontColor(0xFF000000)
          }.width('100%').height('100%').backgroundColor(Color.Pink)
        }
        .tabBar(SubTabBarStyle.of('开始【单行省略号截断单行省略号截断单行省略号截断单行省略号截断单行省略号截断单行省略号截断单行省略号截断单行省略号截断单行省略号截断单行省略号截断】结束')
          .labelStyle({
            overflow: TextOverflow.Ellipsis,
            maxLines: 1,
            minFontSize: 10,
            heightAdaptivePolicy: TextHeightAdaptivePolicy.MAX_LINES_FIRST,
            font: { size: 20 }
          }))

        TabContent() {
          Column() {
            Text('先缩小再截断').fontSize(30).fontColor(0xFF000000)
          }.width('100%').height('100%').backgroundColor(Color.Pink)
        }
        .tabBar(SubTabBarStyle.of('开始【先缩小再截断先缩小再截断先缩小再截断先缩小再截断先缩小再截断先缩小再截断先缩小再截断先缩小再截断先缩小再截断先缩小再截断先缩小再截断先缩小再截断先缩小再截断先缩小再截断】结束')
          .labelStyle({
            overflow: TextOverflow.Clip,
            maxLines: 1,
            minFontSize: 15,
            maxFontSize: 15,
            heightAdaptivePolicy: TextHeightAdaptivePolicy.MIN_FONT_SIZE_FIRST,
            font: { size: 20 }
          }))

        TabContent() {
          Column() {
            Text('先缩小再换行再截断').fontSize(30).fontColor(0xFF000000)
          }.width('100%').height('100%').backgroundColor(Color.Pink)
        }
        .tabBar(SubTabBarStyle.of('开始【先缩小再换行再截断先缩小再换行再截断先缩小再换行再截断先缩小再换行再截断先缩小再换行再截断先缩小再换行再截断先缩小再换行再截断先缩小再换行再截断】结束')
          .labelStyle({
            overflow: TextOverflow.Clip,
            maxLines: 2,
            minFontSize: 15,
            maxFontSize: 15,
            heightAdaptivePolicy: TextHeightAdaptivePolicy.MIN_FONT_SIZE_FIRST,
            font: { size: 20 }
          }))

        TabContent() {
          Column() {
            Text('换行').fontSize(30).fontColor(0xFF000000)
          }
          .width('100%').height('100%').backgroundColor(Color.Pink)
        }.tabBar(SubTabBarStyle.of('开始【换行换行换行换行换行换行换行换行换行换行换行换行换行换行换行】结束')
          .labelStyle({
            overflow: TextOverflow.Clip,
            maxLines: 10,
            minFontSize: 10,
            heightAdaptivePolicy: TextHeightAdaptivePolicy.MAX_LINES_FIRST,
            font: { size: 20 }
          }))
      }
      .vertical(true).scrollable(true)
      .barMode(BarMode.Fixed)
      .barHeight(720)
      .barWidth(200).animationDuration(400)
      .onChange((index: number) => {
        console.info(index.toString())
      })
      .height('100%').width('100%')
    }
    .height('100%')
  }
}

示例6(设置底部页签基本属性)

本示例通过padding、verticalAlign、layoutMode、symmetricExtensible实现了底部页签基本属性的展示。

// xxx.ets
@Entry
@Component
struct TabContentExample6 {
  private controller: TabsController = new TabsController()
  @State text: string = "2"
  @State tabPadding: number = 0;
  @State symmetricExtensible: boolean = false;
  @State layoutMode: LayoutMode = LayoutMode.VERTICAL;
  @State verticalAlign: VerticalAlign = VerticalAlign.Center;

  build() {
    Column() {
      Row() {
        Button("padding+10 " + this.tabPadding)
          .width('47%')
          .height(50)
          .margin({ top: 5 })
          .onClick((event?: ClickEvent) => {
            this.tabPadding += 10
          })
          .margin({ right: '6%', bottom: '12vp' })
        Button("padding-10 " + this.tabPadding)
          .width('47%')
          .height(50)
          .margin({ top: 5 })
          .onClick((event?: ClickEvent) => {
            this.tabPadding -= 10
          })
          .margin({ bottom: '12vp' })
      }

      Row() {
        Button("文本增加 ")
          .width('47%')
          .height(50)
          .margin({ top: 5 })
          .onClick((event?: ClickEvent) => {
            this.text += '文本增加'
          })
          .margin({ right: '6%', bottom: '12vp' })
        Button("文本重置")
          .width('47%')
          .height(50)
          .margin({ top: 5 })
          .onClick((event?: ClickEvent) => {
            this.text = "2"
          })
          .margin({ bottom: '12vp' })
      }

      Row() {
        Button("symmetricExtensible改变 " + this.symmetricExtensible)
          .width('100%')
          .height(50)
          .margin({ top: 5 })
          .onClick((event?: ClickEvent) => {
            this.symmetricExtensible = !this.symmetricExtensible
          })
          .margin({ bottom: '12vp' })
      }

      Row() {
        Button("layoutMode垂直 ")
          .width('47%')
          .height(50)
          .margin({ top: 5 })
          .onClick((event?: ClickEvent) => {
            this.layoutMode = LayoutMode.VERTICAL;
          })
          .margin({ right: '6%', bottom: '12vp' })
        Button("layoutMode水平 ")
          .width('47%')
          .height(50)
          .margin({ top: 5 })
          .onClick((event?: ClickEvent) => {
            this.layoutMode = LayoutMode.HORIZONTAL;
          })
          .margin({ bottom: '12vp' })
      }

      Row() {
        Button("verticalAlign朝上")
          .width('100%')
          .height(50)
          .margin({ top: 5 })
          .onClick((event?: ClickEvent) => {
            this.verticalAlign = VerticalAlign.Top;
          })
          .margin({ bottom: '12vp' })
      }

      Row() {
        Button("verticalAlign居中")
          .width('100%')
          .height(50)
          .margin({ top: 5 })
          .onClick((event?: ClickEvent) => {
            this.verticalAlign = VerticalAlign.Center;
          })
          .margin({ bottom: '12vp' })
      }

      Row() {
        Button("verticalAlign朝下")
          .width('100%')
          .height(50)
          .margin({ top: 5 })
          .onClick((event?: ClickEvent) => {
            this.verticalAlign = VerticalAlign.Bottom;
          })
          .margin({ bottom: '12vp' })
      }



      Tabs({ barPosition: BarPosition.End, controller: this.controller }) {
        TabContent() {
          Column().width('100%').height('100%').backgroundColor(Color.Pink)
        }.tabBar(BottomTabBarStyle.of($r("sys.media.ohos_app_icon"), "1"))

        TabContent() {
          Column().width('100%').height('100%').backgroundColor(Color.Green)
        }.tabBar(BottomTabBarStyle.of($r("sys.media.ohos_app_icon"), this.text)
          .padding(this.tabPadding)
          .verticalAlign(this.verticalAlign)
          .layoutMode(this.layoutMode)
          .symmetricExtensible(this.symmetricExtensible))

        TabContent() {
          Column().width('100%').height('100%').backgroundColor(Color.Blue)
        }.tabBar(BottomTabBarStyle.of($r("sys.media.ohos_app_icon"), "3"))
      }
      .animationDuration(300)
      .height('60%')
      .backgroundColor(0xf1f3f5)
      .barMode(BarMode.Fixed)
    }
    .width('100%')
    .height(500)
    .margin({ top: 5 })
    .padding('24vp')
  }
}

示例7(设置子页签/底部页签文本颜色)

本示例通过labelStyle中的unselectedColor和selectedColor改变底部页签以及子页签的文本颜色。

通过iconStyle中的unselectedColor和selectedColor改变底部页签的图标颜色。

// xxx.ets
@Entry
@Component
struct TabBarStyleExample {
  build() {
    Column({ space: 5 }) {
      Text("子页签样式")
      Column() {
        Tabs({ barPosition: BarPosition.Start }) {
          TabContent() {
            Column().width('100%').height('100%').backgroundColor(Color.Pink)
          }.tabBar(new SubTabBarStyle('Pink')
            .labelStyle({ unselectedColor: Color.Red, selectedColor: Color.Green }))

          TabContent() {
            Column().width('100%').height('100%').backgroundColor(Color.Yellow)
          }.tabBar(new SubTabBarStyle('Yellow')
            .labelStyle({ unselectedColor: Color.Red, selectedColor: Color.Green }))

          TabContent() {
            Column().width('100%').height('100%').backgroundColor(Color.Blue)
          }.tabBar(new SubTabBarStyle('Blue')
            .labelStyle({ unselectedColor: Color.Red, selectedColor: Color.Green }))

          TabContent() {
            Column().width('100%').height('100%').backgroundColor(Color.Green)
          }.tabBar(new SubTabBarStyle('Green')
            .labelStyle({ unselectedColor: Color.Red, selectedColor: Color.Green })
          )
        }
        .vertical(false)
        .scrollable(true)
        .barMode(BarMode.Fixed)
        .onChange((index: number) => {
          console.info(index.toString())
        })
        .width('100%')
        .backgroundColor(0xF1F3F5)
      }.width('100%').height(200)

      Text("底部页签样式")
      Column() {
        Tabs({ barPosition: BarPosition.End }) {
          TabContent() {
            Column().width('100%').height('100%').backgroundColor(Color.Pink)
          }
          .tabBar(new BottomTabBarStyle('/common/public_icon_off.svg', 'pink')
            .labelStyle({ unselectedColor: Color.Red, selectedColor: Color.Green })
            .iconStyle({ unselectedColor: Color.Red, selectedColor: Color.Green })
          )

          TabContent() {
            Column().width('100%').height('100%').backgroundColor(Color.Yellow)
          }.tabBar(new BottomTabBarStyle('/common/public_icon_off.svg', 'Yellow')
            .labelStyle({ unselectedColor: Color.Red, selectedColor: Color.Green })
            .iconStyle({ unselectedColor: Color.Red, selectedColor: Color.Green })
          )

          TabContent() {
            Column().width('100%').height('100%').backgroundColor(Color.Blue)
          }.tabBar(new BottomTabBarStyle('/common/public_icon_off.svg', 'Blue')
            .labelStyle({ unselectedColor: Color.Red, selectedColor: Color.Green })
            .iconStyle({ unselectedColor: Color.Red, selectedColor: Color.Green })
          )

          TabContent() {
            Column().width('100%').height('100%').backgroundColor(Color.Green)
          }.tabBar(new BottomTabBarStyle('/common/public_icon_off.svg', 'Green')
            .labelStyle({ unselectedColor: Color.Red, selectedColor: Color.Green })
            .iconStyle({ unselectedColor: Color.Red, selectedColor: Color.Green })
          )
        }
        .vertical(false)
        .scrollable(true)
        .barMode(BarMode.Fixed)
        .onChange((index: number) => {
          console.info(index.toString())
        })
        .width('100%')
        .backgroundColor(0xF1F3F5)
      }.width('100%').height(200)
    }
  }
}

示例8(设置自定义子页签)

该示例实现了通过ComponentContent设置SubTabBarStyle。

// xxx.ets
import { ComponentContent, UIContext } from "@kit.ArkUI"

class Params {
  text: string = ""

  constructor(text: string) {
    this.text = text;
  }
}

@Builder
function buildText(params: Params) {
  Column() {
    Text(params.text)
      .fontSize(20)
      .fontWeight(FontWeight.Bold)
      .margin(20)
  }
}

@Entry
@Component
struct Index {
  @State message1: string = "tabBar1"
  @State message2: string = "tabBar2"
  context: UIContext = this.getUIContext()
  private count1 = 0;
  private count2 = 0;
  private controller: TabsController = new TabsController();
  tabBar1: ComponentContent<Params> = new ComponentContent<Params>(this.context, wrapBuilder<[Params]>(buildText), new Params(this.message1));
  tabBar2: ComponentContent<Params> = new ComponentContent<Params>(this.context, wrapBuilder<[Params]>(buildText), new Params(this.message2));

  build() {
    Row() {
      Column() {
        Button("更新tabBar1").width('90%').margin(20)
          .onClick((event?: ClickEvent) => {
            this.count1 += 1;
            const message1 = "Update 1_" + this.count1.toString();
            this.tabBar1.update(new Params(message1));
          })
        Button("更新tabBar2").width('90%').margin(20)
          .onClick((event?: ClickEvent) => {
            this.count2 += 1;
            const message2 = "Update 2_" + this.count2.toString();
            this.tabBar2.update(new Params(message2));
          })
        Tabs({ barPosition: BarPosition.Start, controller: this.controller }) {
          TabContent() {
            Column().width('100%').height('100%').backgroundColor(Color.Pink).borderRadius('12vp')
          }.tabBar(new SubTabBarStyle(this.tabBar1))
          TabContent() {
            Column().width('100%').height('100%').backgroundColor(Color.Blue).borderRadius('12vp')
          }.tabBar(SubTabBarStyle.of(this.tabBar2))
        }
        .vertical(false)
        .barWidth(414)
        .barHeight(96)
        .width(414)
        .height(414)
        .backgroundColor('#F1F3F5')
        .margin({ top: 20 })
      }
      .width('100%')
      .height('100%')
    }
    .height('100%')
  }
}

示例9(设置底部页签使用symbol图标)

该示例实现了BottomTabBarStyle图片传入Symbol。

// xxx.ets
import { SymbolGlyphModifier } from '@kit.ArkUI'

@Entry
@Component
struct Index {
  @State symbolModifier1: SymbolGlyphModifier = new SymbolGlyphModifier($r('sys.symbol.ohos_wifi'));
  @State symbolModifier2: SymbolGlyphModifier = new SymbolGlyphModifier($r('sys.symbol.ellipsis_bubble'));
  @State symbolModifier3: SymbolGlyphModifier = new SymbolGlyphModifier($r('sys.symbol.dot_video'));
  @State symbolModifier4: SymbolGlyphModifier = new SymbolGlyphModifier($r('sys.symbol.exposure'));
  build() {
    Column({space: 5}) {
      Text("底部页签样式")
      Column(){
        Tabs({barPosition: BarPosition.End}) {
          TabContent() {
            Column().width('100%').height('100%').backgroundColor(Color.Pink)
          }.tabBar(new BottomTabBarStyle({
            normal: this.symbolModifier1,
          }, 'Pink'))
          .onWillShow(() => {
            console.info("Pink will show")
          })
          .onWillHide(() => {
            console.info("Pink will hide")
          })

          TabContent() {
            Column().width('100%').height('100%').backgroundColor(Color.Orange)
          }.tabBar(new BottomTabBarStyle({
            normal: this.symbolModifier2,
          }, 'Orange'))
          .onWillShow(() => {
            console.info("Orange will show")
          })
          .onWillHide(() => {
            console.info("Orange will hide")
          })

          TabContent() {
            Column().width('100%').height('100%').backgroundColor(Color.Blue)
          }.tabBar(new BottomTabBarStyle({
            normal: this.symbolModifier3,
          }, 'Blue'))
          .onWillShow(() => {
            console.info("Blue will show")
          })
          .onWillHide(() => {
            console.info("Blue will hide")
          })

          TabContent() {
            Column().width('100%').height('100%').backgroundColor(Color.Green)
          }.tabBar(new BottomTabBarStyle({
            normal: this.symbolModifier4,
          }, 'Green'))
          .onWillShow(() => {
            console.info("Green will show")
          })
          .onWillHide(() => {
            console.info("Green will hide")
          })
        }
        .vertical(false)
        .scrollable(true)
        .barMode(BarMode.Fixed)
        .onChange((index:number)=>{
          console.info(index.toString())
        })
        .width('100%')
        .backgroundColor(0xF1F3F5)
      }.width('100%').height(200)
    }
  }
}

【HarmonyOS】【ARKUI】鸿蒙 ets方式tabs+tabcontent 实现底部导航栏 鸿蒙 ets方式tabs+tabcontent 实现底部导航栏 阅读详情

相关推荐

鸿蒙-arkTs:开发工具安装

鸿蒙-arkTs:开发工具(DevEco Studio)安装

Erek的前端博客; 1483

【鸿蒙应用ArkTS开发系列】- 导航栏Tab组件使用讲解

Tabs是一个通过页签进行内容视图切换的容器组件,每个页签对应一个内容视图,它仅可包含子组件TabContent,同时搭配 TabsController使用。TabsController 是Tabs组件的控制器,用于控制Tabs组件进行页签切换。不支持一个TabsController控制多个Tabs组件。具体可查看官网介绍,这里就不多描述。//文本 icon_normal : Resource;// 默认图标 icon_selected : Resource;// 选中图标 }

枫的专栏 1万+

鸿蒙5.0开发进阶:自定义地址选择组件实现案例

本示例介绍如何使用bindSheet,changeIndex,onAreaChange实现带切换动效的自定义地址选择组件。

HSMMQAQ的博客 1197

鸿蒙5.0开发进阶:UI框架-ArkTS组件(Tabs)

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

Androidbye的博客 951

ArkTS开发入门介绍

ArkTS开发,基础知识,入门

别再魔改LWIP了!用STM32CubeMX配置F407的TCP保活(KeepAlive)与自动重连

本文详细介绍了如何通过STM32CubeMX配置STM32F407的LWIP协议栈,实现TCP保活(KeepAlive)与自动重连功能,解决网络异常断开问题。文章提供了从基础环境配置到自动重连架构设计的完整指南,帮助开发者构建高可靠的网络连接机制,适用于工业物联网和嵌入式设备开发。

weixin_27038245的博客 239

鸿蒙HarmonyOS开发环境安装与配置完全解析

华为鸿蒙系统是国内目前纯原生自产的热门移动应用开发系统,经过近几年的完善与优化,具有它特有的应用魅力与实用价值,诚邀国内外广大编程爱好者使用与推广。

sinat_30844883的博客 1万+

Mac 不小心断开移动硬盘导致磁盘无法读取和加载(顺利解决!)

Mac移动硬盘不正确断开连接导致无法读取加载的解决方法,亲测有效

Robin_Pi的博客 1万+

HarmonyOS6 ArkTS TabContent自定义页签切换联动

通过@Builder装饰器封装统一的TabBar样式,实现图标+文字@BuilderColumn() {// 选中/未选中 切换图标// 选中/未选中 切换文字颜色根据判断是否选中自动切换图标和文字颜色所有TabContent共用一套TabBar样式本文基于HarmonyOS6官方TabContent联动规范,在完全不改动原有代码的前提下,完整讲解了自定义TabBar + TabContent页签切换联动使用@Builder封装统一的自定义TabBar样式通过控制图标、文字选中态依靠。

qq_37048082的博客 4159

腾讯云TDSQL MySQL版 - 开发指南兼容性

语言结构 分布式实例支持所有 MySQL 使用的文字格式,包括: String Literals Numeric Literals Date and Time Literals Hexadecimal Literals Bit-Value Literals Boolean Literals NULL Values String Literals String Literals 是一个 bytes 或者 characters 的序列,两端被单引号'或者双引号"包围,TDSQL MySQL版

weixin_43872058的博客 911

从零构建NVENC编码器:一个游戏开发者的实战手册

本文详细介绍了如何从零构建NVENC编码器,特别针对游戏开发者提供实战指南。内容涵盖NVENC基础概念、DirectX纹理捕获、异步编码优化及高级特性配置,帮助开发者高效集成硬件编码技术到游戏引擎中,实现高性能游戏录制和直播功能。重点解析了NVIDIA专用编码芯片的优势与API使用技巧。

weixin_29292431的博客 366

鸿蒙 ArkTS Tabs组件实现类微信(可滑动的)tabBar页签切换页面功能

鸿蒙 ArkTS TabContent组件实现类微信tabBar页签切换页面功能

weixin_44640245的博客 6624

ArcGIS渔网分割实战:用Create Fishnet快速生成1km×1km网格(附GDP数据分析案例)

本文详细解析了ArcGIS中Create Fishnet工具在渔网分割中的应用,从基础操作到GDP空间分析全流程。通过1km×1km网格的创建与优化,实现经济数据的空间标准化和可视化表达,解决数据融合与分析的三大核心痛点。

weixin_29250403的博客 298

鸿蒙应用开发完全指南,涵盖ArkTS语法、UI组件等

《鸿蒙应用开发完全指南》摘要:本文系统介绍了华为HarmonyOS应用开发的学习路径。首先讲解HarmonyOS的分布式架构特点和开发环境搭建,通过"Hello World"示例展示基础开发流程。重点解析ArkTS语言核心语法,包括声明式UI、状态管理等特性,并详细演示常用UI组件(Text/Button/List等)和容器布局(Column/Row/Flex)的使用方法。最后涵盖数据库操作等进阶内容,帮助开发者从零开始掌握鸿蒙应用开发全流程,构建跨设备分布式应用。

woshi_xiaoa的博客 239

Wazuh部署记录

Wazuh是一个免费的开源安全平台,统一了XDR和SIEM功能。它可以保护内部部署、虚拟化、容器化和基于云的环境中的工作负载。Wazuh帮助组织和个人保护其数据资产免受安全威胁。它被全球数千个组织广泛使用,从小型企业到大型企业。443:仪表盘界面访问端口1515:探针连接服务器端口1514:探针回传信息端口以上3个端口必须打开防火墙。

墨痕诉清风的博客 1563

台区同期线损智能诊断与闭环治理体系及工程化实践

高损台区:同期线损率>预设阈值(通常为8%,可根据区域、台区类型调整);负损台区:同期线损率<0,即供电量小于售电量,多由档案、计量、拓扑错误导致;波动损台区:月度线损率波动幅度>预设阈值(通常为5%),且无合理原因;持续异常台区:连续2个及以上周期线损率不达标,且未完成有效整改。台区同期线损智能诊断与闭环治理体系,以电力大数据为基础、智能诊断模型为核心、标准化流程为保障,彻底解决了传统线损治理“异常发现慢、定位难、整改乱、无闭环”的痛点,实现了线损治理从“人工经验驱动”向“数据智能驱动”的根本性转型。

getusushu的博客 205

鸿蒙开发5.0案例分析:ArkUII自定义TabBar组件

由于指示器需要跟随内容视图一起滑动切换,所以指示器不能在单个tabBuilder中设置。1、使用Column组件定义底部指示器,设置一个宽度为文字宽度,高度为3的蓝色指示器;2、这里的指示器宽度可以动态设置成文字的宽度,也可以直接设置成文字某个固定宽度;3、指示器距离左边的距离需要动态设置,配上动画,可以实现指示器跟随手指滑动。Stack() {}.tabBar(this.tabBuilder("房源", 0, $r("app.media.trip_data_start_bg")))...

2501_91726121的博客 666

【系统架构设计师】模拟预测试卷十 - 案例分析(完整结构)

本文为系统架构设计师考试模拟试题及参考答案,包含五个专题:质量属性(必答)、信息系统架构设计、嵌入式系统架构设计、云原生架构设计、面向服务架构设计(SOA)。试题一聚焦非功能需求与质量属性映射,如性能、可修改性、一致性等;其余选答题分别涉及分层架构设计(如嵌入式实时控制)、云原生会话管理、服务化治理(如契约设计、熔断策略)等核心架构场景,涵盖战术应用、权衡分析及解决方案设计。参考答案提供关键要点,例如离线借还的冲突解决、服务编排与共享服务的区分、嵌入式硬实时保护的必要性等,适用于备考实战演练。

数据知道的博客 252

【HarmonyOS NEXT 】应用开发:ArkTS导航组件二(Tabs)

但在使用了自定义导航栏后,使用TabsController可以实现点击页签与页面内容的联动,但不能实现滑动页面时,页面内容对应页签的联动。TabContent是内容页,TabBar是导航页签栏,页面结构如下图所示,根据不同的导航类型,布局会有区别,可以分为底部导航、顶部导航、侧边导航,其导航栏分别位于底部、顶部和侧边。即用户点击对应页签时,屏幕需要显示相应的内容页。对于底部导航栏,一般作为应用主页面功能区分,为了更好的用户体验,会组合文字以及对应语义图标表示页签内容,这种情况下,需要自定义导航页签的样式。

weixin_47351913的博客 2718

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

从零打造鸿蒙 PC AI 应用:HarmonyOS 智慧校园助手开发实战

本文介绍了一款基于鸿蒙PC开发的智慧校园助手应用开发实战。该应用整合了课表管理、待办事项、校园问答和AI对话功能,充分利用鸿蒙PC的大屏优势实现高效信息处理。文章详细阐述了应用的四层架构设计(入口能力层、公共配置层、模型层和页面层),重点讲解了如何通过ArkTS/ArkUI实现大模型接入、本地数据管理和PC端适配优化。开发过程中采用了OpenAI兼容协议、系统提示词工程、响应式UI布局等技术方案,最终在鸿蒙MateBook Pro真机上实现了完整的运行效果,为鸿蒙PC生态的AI应用开发提供了可复用的实践参考

一键难忘的博客 3万+

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

鸿蒙 ArkTS 代码组件

页面顶部 / 底部导航栏,实现多页面切换(比如首页、我的、消息分页)

feird的博客 273

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

鸿蒙系统的组件

• 网络图片:填写图片 URL 地址 常用属性 .width()/.height()、.borderRadius()圆角、.objectFit()填充模式。3.对应文件 ImageDemo1.ets、ImageDemo2.ets、ImageSteck.ets(Stack 叠加图片案例)属性:.fontSize()字号、.fontColor()文字颜色、.fontWeight()加粗。对应文件:Text2.ets、TextDemo.ets、TextDemo2.ets。

Yu7_890的博客 241

【HarmonyOS 6】底部导航实战:Tabs 与玻璃导航栏联动

Tabs 作为内容容器自定义 TabBar 文本样式玻璃拟态底部导航栏使用状态联动保证导航高亮一致掌握这个案例后,你可以轻松扩展为电商首页、课程导航、任务面板等场景。

2503_94458334的博客 1205

【鸿蒙ArkTS】高频基础 UI 组件

本文介绍了HarmonyOS开发中6个常用UI组件的功能与实现方式:1)Swiper轮播图支持自动/手动切换,可配置播放参数;2)Image图片组件支持本地/网络图片,可设置圆角、缩放等效果;3)Tabs选项卡实现多标签页面切换;4)Text/TextInput分别用于文本展示和用户输入;5)Button按钮支持多种样式和交互;6)Radio单选框实现多选一功能。每个组件均提供基本代码示例,展示了在@Entry@Component结构中的典型用法。

2402_89513656的博客 251

鸿蒙原生ArkTS布局方式之Tabs顶部标签导航布局

本文深入解析了鸿蒙ArkTS中Tabs组件实现顶部标签导航布局的技术要点。文章从组件体系、设计理念到实战代码全方位剖析,重点介绍了Tabs容器的核心属性如barPosition、barMode等,并通过示例代码演示了如何构建TabContent内容区域和自定义标签样式。该布局方式符合自然视觉流,能最大化内容展示空间,适用于平级页面切换场景。文中还对比了Tabs与普通滚动布局的区别,强调了Tabs的导航语义特性,为开发者提供了从基础使用到样式定制的完整指南。

2601_95730935的博客 533
上一篇: 鸿蒙5.0开发进阶:UI框架-ArkTS组件(Tabs)
下一篇: 鸿蒙5.0开发进阶:UI框架-ArkTS组件(Button)
懂你的大海
博客等级 码龄6年 9049粉丝 1381原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值