1# 相对布局(RelativeContainer) 2 3 4## 概述 5 6[RelativeContainer](../reference/arkui-ts/ts-container-relativecontainer.md)为采用相对布局的容器,支持容器内部的子元素设置相对位置关系。子元素支持指定兄弟元素作为锚点,也支持指定父容器作为锚点,基于锚点做相对位置布局。下图是一个RelativeContainer的概念图,图中的虚线表示位置的依赖关系。 7 8 9 **图1** 相对布局示意图 10 11 12 13 14子元素并不完全是上图中的依赖关系。比如,Item4可以以Item2为依赖锚点,也可以以RelativeContainer父容器为依赖锚点。 15 16 17## 基本概念 18 19- 锚点:通过锚点设置当前元素基于哪个元素确定位置。 20 21- 对齐方式:通过对齐方式,设置当前元素是基于锚点的上中下对齐,还是基于锚点的左中右对齐。 22 23 24## 设置依赖关系 25 26 27### 锚点设置 28 29锚点设置是指设置子元素相对于父元素或兄弟元素的位置依赖关系。在水平方向上,可以设置left、middle、right的锚点。在竖直方向上,可以设置top、center、bottom的锚点。为了明确定义锚点,必须为RelativeContainer及其子元素设置ID,用于指定锚点信息。ID默认为“__container__”,其余子元素的ID通过id属性设置。未设置ID的子元素在RelativeContainer中不会显示。 30 31>**说明:** 32> 33>在使用锚点时要注意子元素的相对位置关系,避免出现错位或遮挡的情况。 34 35- RelativeContainer父组件为锚点,__container__代表父容器的ID。 36 37 ```ts 38 let AlignRus:Record<string,Record<string,string|VerticalAlign|HorizontalAlign>> = { 39 'top': { 'anchor': '__container__', 'align': VerticalAlign.Top }, 40 'left': { 'anchor': '__container__', 'align': HorizontalAlign.Start } 41 } 42 let AlignRue:Record<string,Record<string,string|VerticalAlign|HorizontalAlign>> = { 43 'top': { 'anchor': '__container__', 'align': VerticalAlign.Top }, 44 'right': { 'anchor': '__container__', 'align': HorizontalAlign.End } 45 } 46 let Mleft:Record<string,number> = { 'left': 20 } 47 let BWC:Record<string,number|string> = { 'width': 2, 'color': '#6699FF' } 48 RelativeContainer() { 49 Row().width(100).height(100) 50 .backgroundColor("#FF3333") 51 .alignRules(AlignRus) 52 .id("row1") 53 54 Row().width(100).height(100) 55 .backgroundColor("#FFCC00") 56 .alignRules(AlignRue) 57 .id("row2") 58 }.width(300).height(300) 59 .margin(Mleft) 60 .border(BWC) 61 ``` 62 63  64 65- 以兄弟元素为锚点。 66 67 ```ts 68 let AlignRus:Record<string,Record<string,string|VerticalAlign|HorizontalAlign>> = { 69 'top': { 'anchor': '__container__', 'align': VerticalAlign.Top }, 70 'left': { 'anchor': '__container__', 'align': HorizontalAlign.Start } 71 } 72 let RelConB:Record<string,Record<string,string|VerticalAlign|HorizontalAlign>> = { 73 'top': { 'anchor': 'row1', 'align': VerticalAlign.Bottom }, 74 'left' : { 'anchor': 'row1', 'align': HorizontalAlign.Start } 75 } 76 let Mleft:Record<string,number> = { 'left': 20 } 77 let BWC:Record<string,number|string> = { 'width': 2, 'color': '#6699FF' } 78 RelativeContainer() { 79 Row().width(100).height(100) 80 .backgroundColor("#FF3333") 81 .alignRules(AlignRus) 82 .id("row1") 83 84 Row().width(100).height(100) 85 .backgroundColor("#FFCC00") 86 .alignRules(RelConB) 87 .id("row2") 88 }.width(300).height(300) 89 .margin(Mleft) 90 .border(BWC) 91 ``` 92 93  94 95 96### 设置相对于锚点的对齐位置 97 98设置了锚点之后,可以通过align设置相对于锚点的对齐位置。 99 100在水平方向上,对齐位置可以设置为HorizontalAlign.Start、HorizontalAlign.Center、HorizontalAlign.End。 101 102 103 104在竖直方向上,对齐位置可以设置为VerticalAlign.Top、VerticalAlign.Center、VerticalAlign.Bottom。 105 106 107 108 109## 场景实例 110 111相对布局内的子元素相对灵活,只要在RelativeContainer容器内,均可以通过alignRules进行相应的位置移动。 112 113 114```ts 115@Entry 116@Component 117struct Index { 118 build() { 119 Row() { 120 RelativeContainer() { 121 Row() 122 .width(100) 123 .height(100) 124 .backgroundColor('#FF3333') 125 .alignRules({ 126 top: { anchor: '__container__', align: VerticalAlign.Top }, //以父容器为锚点,竖直方向顶头对齐 127 middle: { anchor: '__container__', align: HorizontalAlign.Center } //以父容器为锚点,水平方向居中对齐 128 }) 129 .id('row1') //设置锚点为row1 130 131 Row() { 132 Image($r('app.media.icon')) 133 } 134 .height(100).width(100) 135 .alignRules({ 136 top: { anchor: 'row1', align: VerticalAlign.Bottom }, //以row1组件为锚点,竖直方向低端对齐 137 left: { anchor: 'row1', align: HorizontalAlign.Start } //以row1组件为锚点,水平方向开头对齐 138 }) 139 .id('row2') //设置锚点为row2 140 141 Row() 142 .width(100) 143 .height(100) 144 .backgroundColor('#FFCC00') 145 .alignRules({ 146 top: { anchor: 'row2', align: VerticalAlign.Top } 147 }) 148 .id('row3') //设置锚点为row3 149 150 Row() 151 .width(100) 152 .height(100) 153 .backgroundColor('#FF9966') 154 .alignRules({ 155 top: { anchor: 'row2', align: VerticalAlign.Top }, 156 left: { anchor: 'row2', align: HorizontalAlign.End }, 157 }) 158 .id('row4') //设置锚点为row4 159 160 Row() 161 .width(100) 162 .height(100) 163 .backgroundColor('#FF66FF') 164 .alignRules({ 165 top: { anchor: 'row2', align: VerticalAlign.Bottom }, 166 middle: { anchor: 'row2', align: HorizontalAlign.Center } 167 }) 168 .id('row5') //设置锚点为row5 169 } 170 .width(300).height(300) 171 .border({ width: 2, color: '#6699FF' }) 172 } 173 .height('100%').margin({ left: 30 }) 174 } 175} 176``` 177 178 179