鸿蒙(HarmonyOS)项目方舟框架(ArkUI)之SideBarContainer容器组件

鸿蒙(HarmonyOS)项目方舟框架(ArkUI)之SideBarContainer容器组件

一、操作环境

操作系统:  Windows 10 专业版、IDE:DevEco Studio 3.1、SDK:HarmonyOS 3.1+

二、SideBarContainer容器组件

提供侧边栏可以显示和隐藏的侧边栏容器,通过子组件定义侧边栏和内容区,第一个子组件表示侧边栏,第二个子组件表示内容区。

子组件

支持单个子组件。

接口

SideBarContainer( type?: SideBarContainerType )

参数:

参数名

参数类型

必填

参数描述

type

SideBarContainerType

设置侧边栏的显示类型。

默认值:SideBarContainerType.Embed

SideBarContainerType枚举说明

名称

描述

Embed

侧边栏嵌入到组件内,和内容区并列显示。

Overlay

侧边栏浮在内容区上面。

属性

名称

参数类型

描述

showSideBar

boolean

设置是否显示侧边栏。

默认值:true

controlButton

ButtonStyle

设置侧边栏控制按钮的属性。

showControlButton

boolean

设置是否显示控制按钮。

默认值:true

sideBarWidth

number | Length9+

设置侧边栏的宽度。

默认值:200

单位:vp

说明:

设置为小于0的值时按默认值显示。

受最小宽度和最大宽度限制,不在限制区域内取最近的点。

sideBarWidth优先于侧边栏子组件width,sideBarWidth未设置时默认值优先级高于侧边栏子组件width。

minSideBarWidth

number | Length9+

设置侧边栏最小宽度。

默认值:200,单位vp

说明:

设置为小于0的值时按默认值显示。

值不能超过侧边栏容器本身宽度,超过使用侧边栏容器本身宽度。

minSideBarWidth优先于侧边栏子组件minWidth,minSideBarWidth未设置时默认值优先级高于侧边栏子组件minWidth。

maxSideBarWidth

number | Length9+

设置侧边栏最大宽度。

默认值:280,单位vp

说明:

设置为小于0的值时按默认值显示。

值不能超过侧边栏容器本身宽度,超过使用侧边栏容器本身宽度。

maxSideBarWidth优先于侧边栏子组件maxWidth,maxSideBarWidth未设置时默认值优先级高于侧边栏子组件maxWidth。

autoHide9+

boolean

设置当侧边栏拖拽到小于最小宽度后,是否自动隐藏。

默认值:true

说明:

受minSideBarWidth属性方法影响,minSideBarWidth属性方法未设置值使用默认值。

拖拽过程中判断是否要自动隐藏。小于最小宽度时需要阻尼效果触发隐藏(越界一段距离)

sideBarPosition9+

SideBarPosition

设置侧边栏显示位置。

默认值:SideBarPosition.Start

ButtonStyle对象说明

名称

参数类型

必填

描述

left

number

设置侧边栏控制按钮距离容器左界限的间距。

默认值:16

单位:vp

top

number

设置侧边栏控制按钮距离容器上界限的间距。

默认值:48

单位:vp

width

number

设置侧边栏控制按钮的宽度。

默认值:32

单位:vp

height

number

设置侧边栏控制按钮的高度。

默认值:32

单位:vp

icons

{

shown: string | PixelMap | Resource ,

hidden: string | PixelMap | Resource ,

switching?: string | PixelMap | Resource

}

设置侧边栏控制按钮的图标:

- shown: 设置侧边栏显示时控制按钮的图标。

说明:资源获取错误时,使用默认图标。

- hidden: 设置侧边栏隐藏时控制按钮的图标。

- switching:设置侧边栏显示和隐藏状态切换时控制按钮的图标。

SideBarPosition9+枚举说明

名称

描述

Start

侧边栏位于容器左侧。

End

侧边栏位于容器右侧。

事件

名称

功能描述

onChange(callback: (value: boolean) => void)

当侧边栏的状态在显示和隐藏之间切换时触发回调。true表示显示,false表示隐藏。

触发该事件的条件:

1、showSideBar属性值变换时;

2、showSideBar属性自适应行为变化时;

3、分割线拖拽触发autoHide时。

三、示例

代码

// xxx.ets
@Entry
@Component
struct SideBarContainerExample {
  normalIcon: Resource = $r("app.media.icon")
  selectedIcon: Resource = $r("app.media.icon")
  @State arr: number[] = [1, 2, 3]
  @State current: number = 1

  build() {
    SideBarContainer(SideBarContainerType.Embed) {
      Column() {
        ForEach(this.arr, (item, index) => {
          Column({ space: 5 }) {
            Image(this.current === item ? this.selectedIcon : this.normalIcon).width(64).height(64)
            Text("Index0" + item)
              .fontSize(25)
              .fontColor(this.current === item ? '#0A59F7' : '#999')
              .fontFamily('source-sans-pro,cursive,sans-serif')
          }
          .onClick(() => {
            this.current = item
          })
        }, item => item)
      }.width('100%')
      .justifyContent(FlexAlign.SpaceEvenly)
      .backgroundColor('#19000000')



      Column() {
        Text('SideBarContainer content text1').fontSize(25)
        Text('SideBarContainer content text2').fontSize(25)
      }
      .margin({ top: 50, left: 20, right: 30 })
    }
    .controlButton({
      icons: {
        hidden: $r('app.media.drawer'),
        shown: $r('app.media.drawer'),
        switching: $r('app.media.drawer')
      }
    })
    .sideBarWidth(150)
    .minSideBarWidth(50)
    .maxSideBarWidth(300)
    .onChange((value: boolean) => {
      console.info('status:' + value)
    })
  }
}

图例

你有时间常去我家看看我在这里谢谢你啦...

我家地址:亚丁号

最后送大家一首诗:

山高路远坑深,
大军纵横驰奔,

谁敢横刀立马?
惟有点赞加关注大军。

  • 13
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

亚丁号

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值