鸿蒙_使用组件导航Navigation搭建应用框架

组件导航封装了页面、标题、菜单栏、工具栏等功能,我们只需要进行简单的设置,就能快速搭建应用的框架,我们直接新建一个独立页面来通过组件导航实现主页、设置页、我的页三个示例页面,并且相互之间可以跳转,并且天然支持宽屏时主页和二级页面自动分栏显示。

TypeScript 复制代码
@Entry
@Component
struct TestNavigationWithSetting {
  @State message: string = 'Hello World';
  //导航页面栈
  navPathStack: NavPathStack = new NavPathStack();

  //导航页
  @Builder
  navPages(name: string) {
    if (name === 'SettingPage') {
      NavDestination() {
        Text('设置页内容')
        Button('返回')
          .onClick(() => {
            this.navPathStack.pop()
          })
      }.title('设置')
    } else if (name === 'MinePage') {
      NavDestination() {
        Text('我的页内容')
        Button('返回')
          .onClick(() => {
            this.navPathStack.pop()
          })
      }.title('我的')
    }
  }

  //自定义菜单栏
  @Builder
  navMenus() {
    Row({ space: 10 }) {
      Button() {
        Row() {
          SymbolGlyph($r('sys.symbol.house'))
            .fontSize(20)
        }
        .alignItems(VerticalAlign.Center) // 垂直居中
      }
      .padding(10)
      .backgroundColor(Color.White)
      .border({ width: 1 })
      .type(ButtonType.Circle)
      .onClick(() => {
        this.navPathStack.pushPath({ name: 'SettingPage' })
      })

      Button() {
        Row() {
          SymbolGlyph($r('sys.symbol.message'))
            .fontSize(20)
        }
        .alignItems(VerticalAlign.Center) // 垂直居中
      }
      .padding(10)
      .backgroundColor(Color.White)
      .border({ width: 1 })
      .type(ButtonType.Circle)
      .onClick(() => {
        this.navPathStack.pushPath({ name: 'SettingPage' })
      })
    }
    .justifyContent(FlexAlign.End)
    .padding(10)
    //.backgroundColor(Color.Gray)
    .width('100%')
    .height('100%')
  }

  build() {
    Navigation(this.navPathStack) {
      Scroll() {
        Column({ space: 10 }) {
          Text('主页内容')
          Button('跳转到设置页')
            .margin(20)
            .onClick(() => {
              this.navPathStack.pushPath({ name: 'SettingPage' })
            })
          Button('跳转到我的页')
            .onClick(() => {
              this.navPathStack.pushPath({ name: 'MinePage' })
            })

          //生成几个占位
          ForEach([1, 2, 3, 4, 5, 6, 7, 8], (item: number) => {
            Button('多加几个')
              .margin(10)
              .onClick(() => {
                try {
                  this.getUIContext().getPromptAction().showToast({ message: '点击了按钮' })
                } catch (error) {
                  // TODO: Implement error handling.
                }
              })
          })


          Button('最后一个')
            .onClick(() => {
              this.navPathStack.pushPath({ name: 'MinePage' })
            })

        }
        .padding({ top: 10, bottom: 20 })
        .width('100%')
      }
    }
    .navDestination(this.navPages)

    //.mode(NavigationMode.Auto) //导航模式:默认是宽屏(大于600vp)自动分栏
    //.mode(NavigationMode.Stack)//不分栏
    //.mode(NavigationMode.Split)//强制分栏

    .title('主页')
    .title({ main: '主标题', sub: '副标题' })
    //.titleMode(NavigationTitleMode.Free)
    //.titleMode(NavigationTitleMode.Full)
    //.titleMode(NavigationTitleMode.Mini)

    //.hideTitleBar(true) //隐藏标题和菜单栏

    //设置菜单栏
    .menus([
      {
        value: '增加',
        icon: $r('sys.media.ohos_ic_public_add'),
        action: () => {
          this.navPathStack.pushPath({ name: 'MinePage' })
        }
      },
      {
        value: '分享',
        icon: $r('sys.media.ohos_ic_public_share'),
        action: () => {
          this.navPathStack.pushPath({ name: 'SettingPage' })
        }
      },
      {
        value: '编辑',
        icon: $r('sys.media.ohos_ic_public_edit'),
        action: () => {
          this.navPathStack.pushPath({ name: 'MinePage' })
        }
      },
      {
        value: '拍照',
        icon: $r('sys.media.ohos_ic_public_camera'),
        action: () => {
          this.navPathStack.pushPath({ name: 'MinePage' })
        }
      },
      {
        value: '取消',
        icon: $r('sys.media.ohos_ic_public_cancel'),
        action: () => {
          this.navPathStack.pushPath({ name: 'MinePage' })
        }
      }
    ])
    //.menus(this.navMenus) //自定义菜单栏

    .toolbarConfiguration([
      {
        value: '增加',
        icon: $r('sys.media.ohos_ic_public_add'),
        action: () => {
          this.navPathStack.pushPath({ name: 'MinePage' })
        }
      },
      {
        value: '扫码',
        icon: $r('sys.media.ohos_ic_public_scan'),
        action: () => {
          this.navPathStack.pushPath({ name: 'MinePage' })
        }
      },
      {
        value: '拍照',
        icon: $r('sys.media.ohos_ic_public_camera'),
        action: () => {
          this.navPathStack.pushPath({ name: 'SettingPage' })
        }
      }
      /*,
      {
        value: '更多',
        icon: $r('sys.media.ohos_ic_public_more'),
        action: () => {
          this.navPathStack.pushPath({ name: 'MinePage' })
        }
      }*/
    ], { backgroundColor: undefined })

  }
}

实际运行效果如下,横屏效果:

宽屏效果:

相关推荐
风满城3341 分钟前
鸿蒙原生应用实战(三):设置与统计页面开发 — 数据驱动的功能模块
harmonyos
xcLeigh1 小时前
鸿蒙平台 KeePass 密码管理器适配实战:从 Windows 到 鸿蒙PC 的 Electron 迁移指南
windows·electron·web·harmonyos·加密算法·keepass
金启攻1 小时前
鸿蒙原生应用开发实战(一):从零搭建“钓点日记“——项目初始化与环境配置全指南
harmonyos
风华圆舞1 小时前
鸿蒙语音识别为什么要区分 startListening 和 stopListening
华为·语音识别·harmonyos
YM52e1 小时前
鸿蒙PC ArkTS 声明合并问题深度解析与最佳实践
学习·华为·harmonyos·鸿蒙·鸿蒙系统
互联网散修2 小时前
鸿蒙实战:网络状态监听与诊断工具
网络·华为·harmonyos·网络状态监听
祭曦念2 小时前
从零开始构建鸿蒙纪念日提醒 App:ArkTS + API 24 实战
华为·harmonyos
浮芷.3 小时前
鸿蒙HarmonyOS 6.1新特性之沉浸式光感效果实现过程中的各类问题解决-鸿蒙PC版(一)
华为·harmonyos·鸿蒙·鸿蒙系统
轻口味3 小时前
轻规划鸿蒙开发实战7:接管 Ability Kit 跨设备流转,EntryAbility 全链路 onContinue 数据打包与无缝接
华为·harmonyos·鸿蒙
风满城334 小时前
鸿蒙原生应用实战(五):教程、主题与项目总结 — 从开发到上线的完整回顾
harmonyos