鸿蒙(HarmonyOS)应用开发——容器组件(Tabs组件)

前言

前面一篇文章中,已经说了Gridt组件。那么接下来就是容器组件中的Tabs组件
根据功能分类 基础组件 容器组件 媒体组件 绘制组件 画布组件 Column Row List Grid Tabs Swiper

Tabs 介绍

Tabs是一种可以通过页签进行内容视图切换的容器组件,每一个页签对应一个内容视图。Tabs组件必须配合子组件TabContent一起使用

javascript 复制代码
 Tabs({ barPosition: BarPosition, index?:number, controller: TabsController })
  • barPosition
    设置Tabs的页签位置
  • index
    设置初始化页签位置
  • controller
    设置Tabs 控制器,用于控制Tabs组件进行页签切换

属性

vertical

设置Tabs方向是否为纵向,默认为false

javascript 复制代码
Tabs().vertical(boolean?)

barMode

设置布局模式,默认时Fixed

javascript 复制代码
Tabs().barMode(BarMode枚举)
  • BarMode枚举值
枚举值 索命
Fixed 所有TabBar 平均分配barWidth宽度,页签不可滚动
scrollable 所有TabBar 均使用实际布局宽度,页签可滚动

barWidth

页签的宽度

barHeight

页签的高度

设置TabBar位置

Tabs组件页签默认显示在顶部,但是我们可以通过设置TabBar的位置来更改,设置TabBar的位置,与vertical有关

vertical 的值 枚举值 说明
true BarPostiton.Start 页签位于容器顶部
true BarPostiton.end 页签位于容器底部
false BarPostiton.Start 页签位于容器左侧
false BarPostiton.end 页签位于容器右侧
javascript 复制代码
Tabs({ barPosition: BarPosition.Start }) {
  ...
}
.vertical(false) 

自定义TabBar样式

tabBar属性除了支持string类型,还支持使用@Builder装饰器修饰的函数

javascript 复制代码
@Entry
@Component
struct TabsExample {
  @State currentIndex: number = 0;
  private tabsController: TabsController = new TabsController();

  @Builder TabBuilder(title: string, targetIndex: number, selectedImg: Resource, normalImg: Resource) {
    Column() {
      Image(this.currentIndex === targetIndex ? selectedImg : normalImg)
        .size({ width: 25, height: 25 })
      Text(title)
        .fontColor(this.currentIndex === targetIndex ? '#1698CE' : '#6B6B6B')
    }
    .width('100%')
    .height(50)
    .justifyContent(FlexAlign.Center)
    .onClick(() => {
      this.currentIndex = targetIndex;
      this.tabsController.changeIndex(this.currentIndex);
    })
  }

  build() {
    Tabs({ barPosition: BarPosition.End, controller: this.tabsController }) {
      TabContent() {
        Column().width('100%').height('100%').backgroundColor('#00CB87')
      }
      .tabBar(this.TabBuilder('首页', 0, $r('app.media.home_selected'), $r('app.media.home_normal')))

      TabContent() {
        Column().width('100%').height('100%').backgroundColor('#007DFF')
      }
      .tabBar(this.TabBuilder('我的', 1, $r('app.media.mine_selected'), $r('app.media.mine_normal')))
    }
    .barWidth('100%')
    .barHeight(50)
    .onChange((index: number) => {
      this.currentIndex = index;
    })
  }
}
相关推荐
谷子在生长1 天前
纯血鸿蒙自定义弹窗最佳实践:从「到处复制」到「一行调用」
前端·harmonyos
小魔女千千鱼1 天前
把 Go 塞进鸿蒙PC:windows上用 c-shared 跑 2048
harmonyos
TrisighT1 天前
Electron 跑在鸿蒙 PC 上,单窗口和多窗口内存差 800MB?我抓了 5 组数据
性能优化·electron·harmonyos
TrisighT2 天前
AI写埋点代码,35%覆盖率坑惨运营
harmonyos·arkts·arkui
Junerver5 天前
把 DevEco Code 的 HarmonyOS 开发能力装进口袋——harmonyos-dev-skill
harmonyos
程序猿追6 天前
那个右下角的小数字怎么“卡”住我打字——我用 HarmonyOS 自己写了一个字数限制输入框
pytorch·华为·harmonyos
古德new6 天前
鸿蒙PC使用electron迁移:Joplin Electron 桌面适配全记录
华为·electron·harmonyos
世人万千丶6 天前
桌面便签小应用 - HarmonyOS ArkUI 开发实战-TextArea与Flex布局-PC版本
华为·harmonyos·鸿蒙·鸿蒙系统
慧海灵舟6 天前
AGenUI 鸿蒙端实战踩坑录:从 Column 布局消失到异步组件宽度为 0
华为·harmonyos