鸿蒙自定义侧滑菜单布局(DrawerLayout)

前言

为了实现安卓中的侧滑菜单布局效果,通过查找鸿蒙的布局控件,发现SideBarContainer控件有点像,但是使用中发现并不是很符合我们的UI交互效果,因此我在鸿蒙中通过自定义布局的方式实现,本文主要介绍该自定义控件如何使用、SideBarContainer原生控件如何使用,后面会介绍如何通过自定义实现侧滑菜单布局。

DeVEcoStudio版本如下:

复制代码
"minAPIVersion": 9,
"targetAPIVersion": 9,

控件效果

如何使用

1、DrawerLayout.har包的引用

DrawerLayout.har 下载地址:https://download.csdn.net/download/Abner_Crazy/88864397

entry 下的main 目录下新建libs 目录将下载完成的DrawerLayout.har 拷贝到该目录,然后在oh-package.json5 文件中添加对DrawerLayout.har的引用

oh-package.json5文件

复制代码
{
  "license": "",
  "devDependencies": {},
  "author": "",
  "name": "entry",
  "description": "Please describe the basic information.",
  "main": "",
  "version": "1.0.0",
  "dependencies": {
    "@app/DrawerLibrary": "file:./src/main/libs/DrawerLibrary.har"
  }
}

2、调用DrawerLayout布局

复制代码
import { DrawerController, DrawerLayout } from '@app/DrawerLibrary'

@Entry
@Component
struct Index {
   @State controller: DrawerController = new DrawerController()
   @State isShowSideBar: boolean = false

   @Builder
   leftView() {
      Text('我是侧边栏').fontSize(30)
   }

   @Builder
   rightView() {
      Column() {
         RelativeContainer() {
            Image(this.isShowSideBar ? $r('app.media.drawer_select') : $r('app.media.drawer_normal'))
               .width(32)
               .height(32)
               .id('imageDrawer')
               .margin({ left: 20 })
               .alignRules({
                  center: { anchor: '__container__', align: VerticalAlign.Center },
                  left: { anchor: '__container__', align: HorizontalAlign.Start }
               })
               .onClick(() => {
                  if (this.isShowSideBar) {
                     this.controller.hideSideBar()
                  } else {
                     this.controller.showSideBar()
                  }
               })

            Text('首页')
               .fontSize(24)
               .fontColor(Color.White)
               .fontWeight(FontWeight.Bold)
               .id("textTitle")
               .alignRules({
                  center: { anchor: '__container__', align: VerticalAlign.Center },
                  middle: { anchor: '__container__', align: HorizontalAlign.Center }
               })
         }
         .backgroundColor('#1296db')
         .width('100%')
         .height(56)
      }.width('100%')
      .height('100%')
   }

   build() {
      Stack() {
         DrawerLayout({
            isShowSideBar: this.isShowSideBar,
            controller: this.controller,
            leftView: () => this.leftView(),
            rightView: () => this.rightView()
         })
      }.width('100%')
      .height('100%')
   }
}

DrawerLayout参数解释:

|---------------|----------|------------------------------------------------------|
| 属性 | 是否必须 | 描述 |
| isShowSideBar | 是 | 侧边栏是否显示 |
| controller | 是 | 布局控制器,有两个方法: showSideBar():显示侧边栏 hideSideBar():隐藏侧边栏 |
| leftView | 是 | 左侧侧边栏的布局 |
| rightView | 是 | 右侧内容的布局 |
| sideBarWidth | 否 | 左侧侧边栏的宽度 |

鸿蒙原生侧边栏布局使用介绍

SideBarContainer控件官方介绍:sidebarcontainer介绍

1、SideBarContainer调用

复制代码
@Entry
@Component
struct Index {
   @State isShowSideBar: boolean = false

   @Builder
   leftView() {
      Text('我是侧边栏').fontSize(30)
   }

   @Builder
   rightView() {
      Column() {
         RelativeContainer() {
            Image(this.isShowSideBar ? $r('app.media.drawer_select') : $r('app.media.drawer_normal'))
               .width(32)
               .height(32)
               .id('imageDrawer')
               .margin({ left: 20 })
               .alignRules({
                  center: { anchor: '__container__', align: VerticalAlign.Center },
                  left: { anchor: '__container__', align: HorizontalAlign.Start }
               })
               .onClick(() => {
                  this.isShowSideBar = !this.isShowSideBar
               })

            Text('首页')
               .fontSize(24)
               .fontColor(Color.White)
               .fontWeight(FontWeight.Bold)
               .id("textTitle")
               .alignRules({
                  center: { anchor: '__container__', align: VerticalAlign.Center },
                  middle: { anchor: '__container__', align: HorizontalAlign.Center }
               })
         }
         .backgroundColor('#1296db')
         .width('100%')
         .height(56)
      }.width('100%')
      .height('100%')
   }

   build() {
      Stack() {
         SideBarContainer(SideBarContainerType.Embed) {
            this.leftView()

            this.rightView()
         }
         .showSideBar(this.isShowSideBar)
         .showControlButton(false) //是否显示控制按钮
         .sideBarWidth(300)
         .maxSideBarWidth(300)
         .onChange((value: boolean) => {
            console.info('status:' + value)
         })
      }.width('100%')
      .height('100%')
   }
}

2、控件效果:

相关推荐
特立独行的猫a4 天前
使用 vcpkg 为OpenHarmony(鸿蒙PC)构建 OpenSSH 命令行工具
harmonyos·openharmony·命令行·openssh·vcpkg·鸿蒙pc
特立独行的猫a6 天前
HarmonyOS 鸿蒙PC三方库移植:vcpkg方式的 Port 脚本编写简明教程
华为·harmonyos·openharmony·vcpkg·三方库移植
特立独行的猫a7 天前
HarmonyOS / OpenHarmony 平台三方库移植:使用vcpkg 移植 Crashpad 过程实战总结
harmonyos·移植·openharmony·vcpkg·crshpad
特立独行的猫a8 天前
HarmonyOS鸿蒙三方库移植:选 vcpkg 还是 lycium_plusplus?两种“框架化”方案对比
harmonyos·openharmony·vcpkg·三方库移植·鸿蒙pc·lycium_plusplus
左手厨刀右手茼蒿8 天前
Flutter 组件 sheety_localization 的适配 鸿蒙Harmony 实战 - 驾驭在线协作式多语言管理、实现鸿蒙端动态词条下发与全球化敏捷发布方案
flutter·harmonyos·鸿蒙·openharmony·sheety_localization
特立独行的猫a8 天前
使用 vcpkg 为鸿蒙(HarmonyOS / OHOS)下载与安装三方库实践指南
华为·harmonyos·openharmony·vcpkg·三方库·鸿蒙pc
特立独行的猫a10 天前
OpenHarmony平台移植 gifsicle:C/C++ 三方库适配实践(Lycium / tpc_c_cplusplus)
c语言·c++·harmonyos·openharmony·三方库适配·lycium
小菜刀_11 天前
OpenHarmony LiteOS-M LittleFS 文件系统调试与修复实战
openharmony·loongarch·liteos-m
加农炮手Jinx11 天前
Flutter 三方库 better_commit 的鸿蒙化适配指南 - 实现具备语义化提交规范与自动化交互的 Git 工作流插件、支持端侧版本工程的高效规范化审计实战
flutter·harmonyos·鸿蒙·openharmony·better_commit
小菜刀_11 天前
OpenHarmony LiteOS-M Shell 命令开发指南
openharmony·loongarch·liteos-m