HarmonyOS Next 底部 Tab 栏组件开发实战

底部 Tab 导航是移动应用中最常见的交互设计之一,HarmonyOS Next 提供了灵活高效的 Tabs 组件帮助开发者快速实现该功能。本文将通过完整示例讲解如何实现一个高度定制的底部 Tab 栏。

开发步骤详解

步骤 1:创建基础页面结构

javascript 复制代码
@Entry
@Component
struct MainPage {
  @State currentIndex: number = 0 // 当前选中索引

  build() {
    Column() {
      // 内容区域
      this.ContentBuilder()

      // 底部Tab栏
      this.BottomTabs()
    }
    .height('100%')
  }
}

步骤 2:配置 Tab 页面内容

javascript 复制代码
// 内容区域构建器
ContentBuilder() {
  if (this.currentIndex === 0) {
    HomePage()
  } else if (this.currentIndex === 1) {
    DiscoverPage()
  } else {
    MinePage()
  }
}

步骤 3:实现自定义 Tab 栏

javascript 复制代码
// 底部导航构建器
@Builder
BottomTabs() {
  Flex({ justifyContent: FlexAlign.SpaceAround }) {
    this.TabItemBuilder(0, $r('app.media.home'), '首页')
    this.TabItemBuilder(1, $r('app.media.compass'), '发现')
    this.TabItemBuilder(2, $r('app.media.user'), '我的')
  }
  .width('100%')
  .height(60)
  .backgroundColor(Color.White)
  .shadow({ radius: 8, color: '#00000020', offsetX: 0, offsetY: -2 })
}

// 单个Tab项构建器
@Builder
TabItemBuilder(index: number, icon: Resource, text: string) {
  Column() {
    Image(icon)
      .width(24)
      .height(24)
      .objectFit(ImageFit.Contain)
      .fillColor(this.currentIndex === index ? '#FF1949' : '#999')

    Text(text)
      .fontSize(12)
      .fontColor(this.currentIndex === index ? '#FF1949' : '#999')
  }
  .onClick(() => {
    this.currentIndex = index
    // 可添加点击动画
    animateTo({ duration: 100 }, () => {})
  })
}
相关推荐
贾伟康26 分钟前
【共创稿事节】@Link双向绑定的适用边界
性能优化·实战·harmonyos·arkts·arkui
熊猫钓鱼>_>36 分钟前
上架审核常见驳回与隐私合规清单:过审前先自查这 8 条【鸿蒙心迹】
开发语言·前端·华为·华为云·harmonyos·openharmony·ai制作
贾伟康40 分钟前
【共创稿事节】布局审查工具与边界调试
harmonyos·arkui·inspector·性能排查·布局调试
2501_946244781 小时前
开源鸿蒙平台 CMP 三方库「compose-collapsing-toolbar」适配全流程
华为·开源·harmonyos
2401_882351522 小时前
开源鸿蒙平台 CMP 三方库「SegmentedControl」适配全流程
华为·开源·harmonyos
2401_882351523 小时前
开源鸿蒙平台 CMP 三方库「LlmTypewriter」适配全流程
华为·开源·harmonyos
2501_9481226313 小时前
开源鸿蒙平台 CMP 三方库「kmp-showcase-compose」适配全流程
华为·开源·harmonyos
OH_TPC13 小时前
HarmonyOS APP开发---“旅图“旅行摄影社区App,需要用到这个库
华为·harmonyos·鸿蒙
独隅16 小时前
HarmonyOS 系统能力与体验深度评测
华为·harmonyos·harmonyos 7.0
2601_9495758616 小时前
开源鸿蒙平台 CMP 三方库「Persian-Date-picker」适配全流程
华为·开源·harmonyos