ts-container-sidebarcontainer.md 4.6 KB
Newer Older
Y
yaoyuchi 已提交
1 2
# SideBarContainer

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

G
gmy 已提交
5 6 7
>  **说明:**
>
>  该组件从API Version 8开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
Y
yaoyuchi 已提交
8 9 10 11


## 子组件

Y
yaoyuchi 已提交
12
可以包含子组件。
Y
yaoyuchi 已提交
13 14 15 16 17 18


## 接口

SideBarContainer( type?: SideBarContainerType )

G
gmy 已提交
19
**参数:**
Y
yaoyuchi 已提交
20

G
gmy 已提交
21 22 23 24 25 26 27 28
| 参数名 | 参数类型 | 必填 | 参数描述 |
| -------- | -------- | -------- | -------- |
| type | SideBarContainerType | 否 | 设置侧边栏的显示类型。<br/>默认值:SideBarContainerType.Embed |

## SideBarContainerType枚举说明

| 名称 | 描述 |
| -------- | -------- |
T
third  
tianyu 已提交
29
| Embed | 侧边栏嵌入到组件内,和内容区并列显示。 |
G
gmy 已提交
30
| Overlay | 侧边栏浮在内容区上面。 |
Y
yaoyuchi 已提交
31 32 33

## 属性

G
gmy 已提交
34 35 36 37 38
| 名称 | 参数类型 | 描述 |
| -------- | -------- | -------- |
| showSideBar | boolean | 设置是否显示侧边栏。<br/>默认值:true |
| controlButton | ButtonStyle                                                | 设置侧边栏控制按钮的属性。 |
| showControlButton | boolean | 设置是否显示控制按钮。<br/>默认值:true |
L
fix doc  
luoying_ace_admin 已提交
39 40 41
| sideBarWidth | number&nbsp;\|&nbsp;Length<sup>9+</sup> | 设置侧边栏的宽度。<br/>默认值:200,单位vp |
| minSideBarWidth | number&nbsp;\|&nbsp;Length<sup>9+</sup> | 设置侧边栏最小宽度。<br/>默认值:200,单位vp |
| maxSideBarWidth | number&nbsp;\|&nbsp;Length<sup>9+</sup> | 设置侧边栏最大宽度。<br/>默认值:280,单位vp |
G
gmy 已提交
42 43 44 45 46 47
| autoHide<sup>9+</sup> | boolean | 设置当侧边栏拖拽到小于最小宽度后,是否自动隐藏。<br/>默认值:true |
| sideBarPosition<sup>9+</sup> | SideBarPosition | 设置侧边栏显示位置。<br/>默认值:SideBarPosition.Start |

## ButtonStyle对象说明

| 名称 | 参数类型 | 必填 | 描述 |
Y
yaoyuchi 已提交
48
| -------- | -------- | -------- | -------- |
T
third  
tianyu 已提交
49 50 51 52
| left | number | 否 | 设置侧边栏控制按钮距离容器左界限的间距。<br/>默认值:16,单位vp |
| top | number | 否 | 设置侧边栏控制按钮距离容器上界限的间距。<br/>默认值:48,单位vp |
| width | number | 否 | 设置侧边栏控制按钮的宽度。<br/>默认值:32,单位vp |
| height | number | 否 | 设置侧边栏控制按钮的高度。<br/>默认值:32,单位vp |
H
HelloCrease 已提交
53
| icons | {<br/>shown:&nbsp;string \| PixelMap \| [Resource](ts-types.md) ,<br/>hidden:&nbsp;string \| PixelMap \| [Resource](ts-types.md) ,<br/>switching?:&nbsp;string \| PixelMap \| [Resource](ts-types.md) <br/>} | 否 | 设置侧边栏控制按钮的图标:<br/> </p> - shown: 设置侧边栏显示时控制按钮的图标。<br>- hidden: 设置侧边栏隐藏时控制按钮的图标。<br>- switching:设置侧边栏显示和隐藏状态切换时控制按钮的图标。 |
Y
yaoyuchi 已提交
54

G
gmy 已提交
55
## SideBarPosition<sup>9+</sup>枚举说明
Y
yaoyuchi 已提交
56

G
gmy 已提交
57 58 59 60
| 名称 | 描述 |
| -------- | -------- |
| Start | 侧边栏位于容器左侧。 |
| End | 侧边栏位于容器右侧。 |
Y
yaoyuchi 已提交
61 62 63

## 事件

G
gmy 已提交
64
| 名称 | 功能描述 |
Y
yaoyuchi 已提交
65
| -------- | -------- |
T
third  
tianyu 已提交
66
| onChange(callback: (value: boolean) =&gt; void) | 当侧边栏的状态在显示和隐藏之间切换时触发回调。<p> true表示显示,false表示隐藏。 |
Y
yaoyuchi 已提交
67 68 69 70


## 示例

H
geshi  
HelloCrease 已提交
71 72
```ts
// xxx.ets
Y
yaoyuchi 已提交
73 74 75
@Entry
@Component
struct SideBarContainerExample {
X
xuzhidan 已提交
76 77
  normalIcon : Resource = $r("app.media.icon")
  selectedIcon: Resource = $r("app.media.icon")
Y
yaoyuchi 已提交
78 79 80 81
  @State arr: number[] = [1, 2, 3]
  @State current: number = 1

  build() {
Y
yaoyuchi 已提交
82
    SideBarContainer(SideBarContainerType.Embed)
Y
yaoyuchi 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    {
      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')
X
xuzhidan 已提交
100 101 102 103 104 105 106


      Column() {
        Text('SideBarContainer content text1').fontSize(25)
        Text('SideBarContainer content text2').fontSize(25)
      }
      .margin({ top: 50, left: 20, right: 30 })
Y
yaoyuchi 已提交
107
    }
X
xuzhidan 已提交
108 109 110
    .sideBarWidth(150)
    .minSideBarWidth(50)
    .maxSideBarWidth(300)
Y
yaoyuchi 已提交
111 112 113 114 115 116
    .onChange((value: boolean) => {
      console.info('status:' + value)
    })
  }
}
```
Y
yaoyuchi 已提交
117 118

![](figures/sidebarcontainer.png)