HarmonyOS Next鸿蒙ArkUI封装通用标题栏组件

1,006 阅读2分钟

近期一直在研究HarmonyOS鸿蒙开发,基于纯血鸿蒙Next 5.0 API12自定义了一款加强版顶部导航条组件。

image.png

HMNavBar自定义标题栏组件支持返回键、标题/副标题、标题居中、背景色/背景图片/背景渐变色、标题颜色、搜索、右侧操作按钮等功能。

image.png

p1.gif

组件目录如下:

059e369873a0e442eca9b5de11b3c0b5_1289798-20241031124859083-39238132.png

支持如下参数自定义配置:

@Component
export struct HMNavBar {
  // 是否隐藏左侧的返回键
  @Prop hideBackButton: boolean
  // 标题(支持字符串|自定义组件)
  @BuilderParam title: ResourceStr | CustomBuilder = BuilderFunction
  // 副标题
  @BuilderParam subtitle: ResourceStr | CustomBuilder = BuilderFunction
  // 返回键图标
  @Prop backButtonIcon: Resource | undefined = $r('sys.symbol.chevron_left')
  // 返回键标题
  @Prop backButtonTitle: ResourceStr
  // 背景色
  @Prop bgColor: ResourceColor = $r('sys.color.background_primary')
  // 渐变背景色
  @Prop bgLinearGradient: LinearGradient
  // 图片背景
  @Prop bgImage: ResourceStr | PixelMap
  // 标题颜色
  @Prop fontColor: ResourceColor
  // 标题是否居中
  @Prop centerTitle: boolean
  // 右侧按钮区域
  @BuilderParam actions: Array<ActionMenuItem> | CustomBuilder = BuilderFunction
  // 导航条高度
  @Prop navbarHeight: number = 56

  // ...
}

360截图20241031135550843.png

调用方式非常简单,引入组件,然后直接通过如下方式使用即可。

2ff8c6f0b12ac24324b091159ad23782_1289798-20241031130138165-1440903000.png

  • 基础用法
HMNavBar({
  backButtonIcon: $r('sys.symbol.arrow_left'),
  title: '鸿蒙自定义导航栏',
  subtitle: 'HarmonyOS Next 5.0自定义导航栏',
})
  • 进阶用法

自定义背景、标题、右侧操作按钮。

@Builder customImgTitle() {
  Image('https://developer.huawei.com/allianceCmsResource/resource/HUAWEI_Developer_VUE/images/logo-developer-header.svg').height(24).objectFit(ImageFit.Contain)
}

HMNavBar({
  backButtonIcon: $r('sys.symbol.chevron_left'),
  backButtonTitle: '返回',
  title: this.customImgTitle,
  subtitle: '鸿蒙5.0 api 12',
  centerTitle: true,
  bgColor: '#f3f6f9',
  fontColor: '#0a59f7',
  actions: [
    {
      icon: $r('app.media.app_icon'),
      action: () => promptAction.showToast({ message: "show toast index 1" })
    },
    {
      // icon: $r('sys.symbol.plus'),
      label: '更多>',
      color: '#bd43ff',
      action: () => promptAction.showToast({ message: "show toast index 2" })
    }
  ]
})

自定义渐变背景、背景图片、右侧操作区。

HMNavBar({
  hideBackButton: true,
  title: 'HarmonyOS',
  subtitle: 'harmonyos next 5.0 api 12',
  bgLinearGradient: {
    angle: 135,
    colors: [['#42d392 ',0.2], ['#647eff',1]]
  },
  // bgImage: 'pages/assets/nav_bg.png',
  // bgImage: 'https://developer.huawei.com/allianceCmsResource/resource/HUAWEI_Developer_VUE/images/1025-pc-banner.jpeg',
  fontColor: '#fff',
  actions: [
    {
      icon: 'https://developer.huawei.com/allianceCmsResource/resource/HUAWEI_Developer_VUE/images/yuanfuwuicon.png',
      action: () => promptAction.showToast({ message: "show toast index 1" })
    },
    {
      icon: 'https://developer.huawei.com/allianceCmsResource/resource/HUAWEI_Developer_VUE/images/0620logo4.png',
      action: () => promptAction.showToast({ message: "show toast index 2" })
    },
    {
      icon: $r('sys.symbol.person_crop_circle_fill_1'),
      action: () => promptAction.showToast({ message: "show toast index 3" })
    }
  ],
  navbarHeight: 70
})

a19be07d5afaed69b6b05114430487bf_1289798-20241031130350285-1578937772.png

还支持自定义标题栏搜索功能。

HMNavBar({
  title: this.customSearchTitle1,
  actions: this.customSearchAction
})

HMNavBar({
  hideBackButton: true,
  title: this.customSearchTitle2,
  bgColor: '#0051ff',
})

HMNavBar({
  backButtonIcon: $r('sys.symbol.arrow_left'),
  backButtonTitle: '鸿蒙',
  title: this.customSearchTitle3,
  bgColor: '#c543fd',
  fontColor: '#fff',
  actions: [
    {
      icon: $r('sys.symbol.mic_fill'),
      action: () => promptAction.showToast({ ... })
    }
  ]
})

360截图20241031135908690.png

HMNavBar导航布局结构

build() {
    Flex() {
      // 左侧模块
      Stack({ alignContent: Alignment.Start }) {
        Flex(){
          if(!this.hideBackButton) {
            this.backBuilder()
          }
          if(!this.centerTitle) {
            this.contentBuilder()
          }
        }
        .height('100%')
      }
      .height('100%')
      .layoutWeight(1)

      // 中间模块
      if(this.centerTitle) {
        Stack() {
          this.contentBuilder()
        }
        .height('100%')
        .layoutWeight(1)
      }

      // 右键操作模块
      Stack({ alignContent: Alignment.End }) {
        this.actionBuilder()
      }
      .padding({ right: 16 })
      .height('100%')
      .layoutWeight(this.centerTitle ? 1 : 0)
    }
    .backgroundColor(this.bgColor)
    .linearGradient(this.bgLinearGradient)
    .backgroundImage(this.bgImage, ImageRepeat.NoRepeat).backgroundImageSize(ImageSize.FILL)
    .height(this.navbarHeight)
    .width('100%')
}

eade151d6481edda4c3fbd9b32094c42_1289798-20241031130934642-2124921952.png

如上图:支持透明镂空导航条悬浮在背景大图上面。

OK,综上就是HarmonyOS自定义导航条的一些知识分享,希望对大家有所帮助哈~

juejin.cn/post/742565…

juejin.cn/post/741924…

juejin.cn/post/741066…

juejin.cn/post/738921…

duitang.gif