1# SideBarContainer 2 3提供侧边栏可以显示和隐藏的侧边栏容器,通过子组件定义侧边栏和内容区,第一个子组件表示侧边栏,第二个子组件表示内容区。 4 5> **说明:** 6> 7> 该组件从API Version 8开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。 8 9 10## 子组件 11 12可以包含子组件。 13 14 15## 接口 16 17SideBarContainer( type?: SideBarContainerType ) 18 19**参数:** 20 21| 参数名 | 参数类型 | 必填 | 参数描述 | 22| ------ | -------------------- | ---- | ------------------------------------------------------------ | 23| type | SideBarContainerType | 否 | 设置侧边栏的显示类型。<br/>默认值:SideBarContainerType.Embed | 24 25## SideBarContainerType枚举说明 26 27| 名称 | 描述 | 28| ------- | ---------------------- | 29| Embed | 侧边栏嵌入到组件内,侧边栏和内容区并列显示。 | 30| Overlay | 侧边栏浮在内容区上面。 | 31 32## 属性 33 34| 名称 | 参数类型 | 描述 | 35| ----------------- | ----------- | -------------------------------------------- | 36| showSideBar | boolean | 设置是否显示侧边栏。<br/>默认值:true | 37| controlButton | ButtonStyle | 设置侧边栏控制按钮的属性。 | 38| showControlButton | boolean | 设置是否显示控制按钮。<br/>默认值:true | 39| sideBarWidth | number | 设置侧边栏的宽度。<br/>默认值:200,单位vp | 40| minSideBarWidth | number | 设置侧边栏最小宽度。<br/>默认值:200,单位vp | 41| maxSideBarWidth | number | 设置侧边栏最大宽度。<br/>默认值:280,单位vp | 42 43## ButtonStyle对象说明 44 45| 名称 | 参数类型 | 必填 | 描述 | 46| ------ | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 47| left | number | 否 | 设置侧边栏控制按钮距离容器左界限的间距。<br/>默认值:16,单位vp | 48| top | number | 否 | 设置侧边栏控制按钮距离容器上界限的间距。<br/>默认值:48,单位vp | 49| width | number | 否 | 设置侧边栏控制按钮的宽度。<br/>默认值:32,单位vp | 50| height | number | 否 | 设置侧边栏控制按钮的高度。<br/>默认值:32,单位vp | 51| icons | {<br/>shown: string \| PixelMap \| [Resource](ts-types.md#resource) ,<br/>hidden: string \| PixelMap \| [Resource](ts-types.md#resource) ,<br/>switching?: string \| PixelMap \| [Resource](ts-types.md#resource) <br/>} | 否 | 设置侧边栏控制按钮的图标:<br/> </p> - shown: 设置侧边栏显示时控制按钮的图标。<br>- hidden: 设置侧边栏隐藏时控制按钮的图标。<br>- switching:设置侧边栏显示和隐藏状态切换时控制按钮的图标。 | 52 53 54 55## 事件 56 57| 名称 | 功能描述 | 58| -------------------------------------- | ------------------------------------------------------------ | 59| onChange(callback: boolean) => void | 当侧边栏的状态在显示和隐藏之间切换时触发回调。<p> true表示显示,false表示隐藏。 | 60 61 62## 示例 63 64```ts 65// xxx.ets 66@Entry 67@Component 68struct SideBarContainerExample { 69 normalIcon : Resource = $r("app.media.user") 70 selectedIcon: Resource = $r("app.media.userFull") 71 @State arr: number[] = [1, 2, 3] 72 @State current: number = 1 73 74 build() { 75 SideBarContainer(SideBarContainerType.Embed) 76 { 77 Column() { 78 ForEach(this.arr, (item, index) => { 79 Column({ space: 5 }) { 80 Image(this.current === item ? this.selectedIcon : this.normalIcon).width(64).height(64) 81 Text("Index0" + item) 82 .fontSize(25) 83 .fontColor(this.current === item ? '#0A59F7' : '#999') 84 .fontFamily('source-sans-pro,cursive,sans-serif') 85 } 86 .onClick(() => { 87 this.current = item 88 }) 89 }, item => item) 90 }.width('100%') 91 .justifyContent(FlexAlign.SpaceEvenly) 92 .backgroundColor('#19000000') 93 RowSplit() { 94 Column(){ 95 Text('Split page one').fontSize(30) 96 }.justifyContent(FlexAlign.Center) 97 Column(){ 98 Text('Split page two').fontSize(30) 99 }.justifyContent(FlexAlign.Center) 100 }.width('100%') 101 } 102 .sideBarWidth(240) 103 .minSideBarWidth(210) 104 .maxSideBarWidth(260) 105 .onChange((value: boolean) => { 106 console.info('status:' + value) 107 }) 108 } 109} 110``` 111 112 113