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

打印 上一主题 下一主题

主题 1056|帖子 1056|积分 3168

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
媒介

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

  1. "minAPIVersion": 9,
  2. "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文件
  1. {
  2.   "license": "",
  3.   "devDependencies": {},
  4.   "author": "",
  5.   "name": "entry",
  6.   "description": "Please describe the basic information.",
  7.   "main": "",
  8.   "version": "1.0.0",
  9.   "dependencies": {
  10.     "@app/DrawerLibrary": "file:./src/main/libs/DrawerLibrary.har"
  11.   }
  12. }
复制代码
2、调用DrawerLayout布局

  1. import { DrawerController, DrawerLayout } from '@app/DrawerLibrary'
  2. @Entry
  3. @Component
  4. struct Index {
  5.    @State controller: DrawerController = new DrawerController()
  6.    @State isShowSideBar: boolean = false
  7.    @Builder
  8.    leftView() {
  9.       Text('我是侧边栏').fontSize(30)
  10.    }
  11.    @Builder
  12.    rightView() {
  13.       Column() {
  14.          RelativeContainer() {
  15.             Image(this.isShowSideBar ? $r('app.media.drawer_select') : $r('app.media.drawer_normal'))
  16.                .width(32)
  17.                .height(32)
  18.                .id('imageDrawer')
  19.                .margin({ left: 20 })
  20.                .alignRules({
  21.                   center: { anchor: '__container__', align: VerticalAlign.Center },
  22.                   left: { anchor: '__container__', align: HorizontalAlign.Start }
  23.                })
  24.                .onClick(() => {
  25.                   if (this.isShowSideBar) {
  26.                      this.controller.hideSideBar()
  27.                   } else {
  28.                      this.controller.showSideBar()
  29.                   }
  30.                })
  31.             Text('首页')
  32.                .fontSize(24)
  33.                .fontColor(Color.White)
  34.                .fontWeight(FontWeight.Bold)
  35.                .id("textTitle")
  36.                .alignRules({
  37.                   center: { anchor: '__container__', align: VerticalAlign.Center },
  38.                   middle: { anchor: '__container__', align: HorizontalAlign.Center }
  39.                })
  40.          }
  41.          .backgroundColor('#1296db')
  42.          .width('100%')
  43.          .height(56)
  44.       }.width('100%')
  45.       .height('100%')
  46.    }
  47.    build() {
  48.       Stack() {
  49.          DrawerLayout({
  50.             isShowSideBar: this.isShowSideBar,
  51.             controller: this.controller,
  52.             leftView: () => this.leftView(),
  53.             rightView: () => this.rightView()
  54.          })
  55.       }.width('100%')
  56.       .height('100%')
  57.    }
  58. }
复制代码
DrawerLayout参数表明:
属性是否必须描述
isShowSideBar侧边栏是否显示
controller 布局控制器,有两个方法:
showSideBar():显示侧边栏
hideSideBar():隐蔽侧边栏
leftView左侧侧边栏的布局
rightView右侧内容的布局
sideBarWidth左侧侧边栏的宽度
















鸿蒙原生侧边栏布局使用先容

SideBarContainer控件官方先容:sidebarcontainer先容

1、SideBarContainer调用

  1. @Entry
  2. @Component
  3. struct Index {
  4.    @State isShowSideBar: boolean = false
  5.    @Builder
  6.    leftView() {
  7.       Text('我是侧边栏').fontSize(30)
  8.    }
  9.    @Builder
  10.    rightView() {
  11.       Column() {
  12.          RelativeContainer() {
  13.             Image(this.isShowSideBar ? $r('app.media.drawer_select') : $r('app.media.drawer_normal'))
  14.                .width(32)
  15.                .height(32)
  16.                .id('imageDrawer')
  17.                .margin({ left: 20 })
  18.                .alignRules({
  19.                   center: { anchor: '__container__', align: VerticalAlign.Center },
  20.                   left: { anchor: '__container__', align: HorizontalAlign.Start }
  21.                })
  22.                .onClick(() => {
  23.                   this.isShowSideBar = !this.isShowSideBar
  24.                })
  25.             Text('首页')
  26.                .fontSize(24)
  27.                .fontColor(Color.White)
  28.                .fontWeight(FontWeight.Bold)
  29.                .id("textTitle")
  30.                .alignRules({
  31.                   center: { anchor: '__container__', align: VerticalAlign.Center },
  32.                   middle: { anchor: '__container__', align: HorizontalAlign.Center }
  33.                })
  34.          }
  35.          .backgroundColor('#1296db')
  36.          .width('100%')
  37.          .height(56)
  38.       }.width('100%')
  39.       .height('100%')
  40.    }
  41.    build() {
  42.       Stack() {
  43.          SideBarContainer(SideBarContainerType.Embed) {
  44.             this.leftView()
  45.             this.rightView()
  46.          }
  47.          .showSideBar(this.isShowSideBar)
  48.          .showControlButton(false) //是否显示控制按钮
  49.          .sideBarWidth(300)
  50.          .maxSideBarWidth(300)
  51.          .onChange((value: boolean) => {
  52.             console.info('status:' + value)
  53.          })
  54.       }.width('100%')
  55.       .height('100%')
  56.    }
  57. }
复制代码
2、控件结果:
 


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

自由的羽毛

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表