• 鸿蒙自定义侧滑菜单布局(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文件

    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、控件效果:

     

  • 相关阅读:
    Java 17 VS Java 8: 新旧对决,这些Java 17新特性你不容错过
    什么是layer1,layer2,为什么区块链需要layer2?
    Kafka - topic producer consumer 常用命令
    电子学会青少年软件编程 Python编程等级考试三级真题解析(判断题)2020年12月
    django 的 filter 使用技巧
    【软件测试】软件测试基础理论
    Java学习笔记——并发编程(三)
    以Vivado工具为例了解FPGA综合
    信息论学习笔记(一):认识通信系统
    C++ multimap实践
  • 原文地址:https://blog.csdn.net/Abner_Crazy/article/details/136234674