【入门到实战】OpenHarmony开发:NavBar 导航栏

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


介绍

为页面提供导航功能,常用于页面顶部

NavBar 导航栏

导入依赖

import NavBar from "@peakmain/library/src/main/ets/components/title/NavBar"

参数

参数名参数类型描述
titlestring设置导航栏标题
titleBoldnumber | FontWeight | string字体粗细,默认是FontWeight.Bold
titleColorResourceColor设置标题字体颜色,默认字体颜色是 #272a2b
backgroundColorResourceResourceColor设置背景颜色,默认背景颜色是 Color.White
leftImagePixelMap | ResourceStr | DrawableDescriptor | null设置左边返回箭头资源,为空时不显示
rightstring右边文本内容
rightImagePixelMap | ResourceStr | DrawableDescriptor | null设置右边图片资源,为空时不显示
rightClick() => void设置右边图片或者文本的点击事件
leftClickvoid = ()左边返回点击事件,默认处理是返回上一个页面
showLeftCloseboolean是否显示关闭按钮,默认为false

示例

 import { promptAction } from '@kit.ArkUI';
import { NavBar } from '@peakmain/library';

@Entry
@Component
struct NavBarPage {
  @State message: string = 'Hello World';

  @Builder
  ButtonBuilder(title: string, itemClick: () => void = () => {
  }) {
    Row() {
      Text(title).fontColor($r("app.color.color_272a2b"))
        .layoutWeight(1).fontSize(16)
    }
    .width("100%")
    .padding(20)
    .backgroundColor(Color.White)
    .borderRadius(8)
    .onClick(() => {
      itemClick && itemClick()
    })
    .margin({
      top: 1
    })
    .stateStyles({
      normal: {
        .backgroundColor(Color.White)
      },
      pressed: {
        .backgroundColor($r("app.color.color_f1f3f5"))
      }
    })
  }

  @State
  title: string = "默认样式"
  @State
  leftImage: PixelMap | ResourceStr | DrawableDescriptor | null = $r("app.media.ic_public_left_arrow")
  @State
   titleBold: FontWeight = FontWeight.Bold
  @State
  rightText: string = ""
  @State
  rightImage: PixelMap | ResourceStr | DrawableDescriptor | null = null
  rightClick: () => void = () => {
    promptAction.showToast({
      message: "点击了右边按钮"
    })
  }

  build() {
    Column() {
      NavBar({
        title: this.title,//标题
        leftImage: this.leftImage,//左边返回图片
        titleBold: this.titleBold,//标题是否加粗
        right: this.rightText,//右边文本,默认为空
        rightClick: this.rightClick,//右边按钮的点击事件
        rightImage: this.rightImage//右边图片
      })
      //修改左边箭头
      this.ButtonBuilder(`隐藏/显示左边返回箭头,默认显示`, () => {
        this.leftImage = this.leftImage ? null : $r("app.media.ic_public_left_arrow")
      })
      this.ButtonBuilder("替换左边返回箭头", () => {
        this.leftImage = $r("app.media.ic_new_left_arrow")
      })
      //修改标题
      this.ButtonBuilder("修改标题为:NavBar 标题组件", () => {
        this.title = "NavBar 标题组件"
      })
      this.ButtonBuilder("标题是否加粗,默认是加粗", () => {
        this.titleBold = this.titleBold == FontWeight.Bold ? FontWeight.Normal : FontWeight.Bold
      })
      //修改右边文本/菜单
      this.ButtonBuilder("显示右边文本为清空,默认不显示", () => {
        this.rightText = "清空"
      })
      this.ButtonBuilder("显示并设置右边图片按钮,默认不显示", () => {
        this.rightImage = $r("app.media.ic_right_menu")
      })


    }
    .width("100%")
    .height("100%")
    .backgroundColor($r("app.color.color_f1f3f5"))
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值