1# 位置设置 2 3设置组件的对齐方式、布局方向和显示位置。 4 5> **说明:** 6> 7> 从API Version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。 8 9## align 10 11align(value: Alignment) 12 13设置容器元素绘制区域内的子元素的对齐方式。 14 15**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 16 17**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 18 19**系统能力:** SystemCapability.ArkUI.ArkUI.Full 20 21**参数:** 22 23| 参数名 | 类型 | 必填 | 说明 | 24| ------ | ------------------------------------------- | ---- | ------------------------------------------------------------ | 25| value | [Alignment](ts-appendix-enums.md#alignment) | 是 | 设置容器元素绘制区域内的子元素的对齐方式。<br/>只在Stack、Button、Marquee、StepperItem、Text、TextArea、TextInput、FolderStack中生效,其中和文本相关的组件Marquee、Text、TextArea、TextInput的align结果参考[textAlign](ts-basic-components-text.md#textalign)。<br/>不支持textAlign属性的组件则无法设置水平方向的文字对齐。<br/>默认值:Alignment.Center<br/>**说明:** <br/>该属性不支持镜像能力。<br/>在Stack中该属性与alignContent效果一致,只能设置子组件在容器内的对齐方式。 | 26 27## direction 28 29direction(value: Direction) 30 31设置容器元素内主轴方向上的布局。 32 33**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 34 35**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 36 37**系统能力:** SystemCapability.ArkUI.ArkUI.Full 38 39**参数:** 40 41| 参数名 | 类型 | 必填 | 说明 | 42| ------ | ------------------------------------------- | ---- | --------------------------------------------------- | 43| value | [Direction](ts-appendix-enums.md#direction) | 是 | 设置容器元素内主轴方向上的布局。<br/>属性配置为auto的时候,按照系统语言方向进行布局。<br/>该属性在Column组件上不生效。<br/>默认值:Direction.Auto | 44 45## position 46 47position(value: Position | Edges | LocalizedEdges) 48 49绝对定位,确定子组件相对父组件的位置。 50 51**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 52 53**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 54 55**系统能力:** SystemCapability.ArkUI.ArkUI.Full 56 57**参数:** 58 59| 参数名 | 类型 | 必填 | 说明 | 60| ------ | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 61| value | [Position](ts-types.md#position) \| [Edges<sup>12+</sup>](ts-types.md#edges12) \| [LocalizedEdges<sup>12+</sup>](ts-types.md#localizededges12) | 是 | 绝对定位,确定子组件相对父组件的位置。当父容器为Row/Column/Flex时,设置position的子组件不占位。<br/>Position类型基于父组件左上角确定位置;Edges类型基于父组件四边确定位置,top/left/right/bottom分别为组件各边距离父组件相应边的边距,通过边距来确定组件相对于父组件的位置;LocalizedEdges类型基于父组件四边确定位置,支持镜像模式。<br/>适用于置顶显示、悬浮按钮等组件在父容器中位置固定的场景。<br/>不支持在宽高为零的布局容器上设置。<br/>当父容器为[RelativeContainer](ts-container-relativecontainer.md), 且子组件设置了alignRules属性, 则子组件的position属性不生效。 | 62 63## markAnchor 64 65markAnchor(value: Position | LocalizedPosition) 66 67设置元素在位置定位时的锚点。 68 69**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 70 71**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 72 73**系统能力:** SystemCapability.ArkUI.ArkUI.Full 74 75**参数:** 76 77| 参数名 | 类型 | 必填 | 说明 | 78| ------ | -------------------------------- | ---- | ------------------------------------------------------------ | 79| value | [Position](ts-types.md#position) \| [LocalizedPosition<sup>12+</sup>](ts-types.md#localizedposition12) | 是 | 设置元素在位置定位时的锚点,从position或offset的位置上,进一步偏移。<br/>设置.position({x: value1, y: value2}).markAnchor({x: value3, y: value4}),效果等于设置.position({x: value1 - value3, y: value2 - value4}),offset同理。<br/>单独使用markAnchor,设置.markAnchor({x: value1, y: value2}),效果等于设置.offset({x: -value1, y: -value2})。<br/>API version 9及以前,默认值为:<br/>{<br/>x: 0,<br/>y: 0<br/>}<br/>API version 10:无默认值。 | 80 81## offset 82 83offset(value: Position | Edges | LocalizedEdges) 84 85相对偏移,组件相对原本的布局位置进行偏移。 86 87**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 88 89**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 90 91**系统能力:** SystemCapability.ArkUI.ArkUI.Full 92 93**参数:** 94 95| 参数名 | 类型 | 必填 | 说明 | 96| ------ | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 97| value | [Position](ts-types.md#position) \| [Edges<sup>12+</sup>](ts-types.md#edges12) \| [LocalizedEdges<sup>12+</sup>](ts-types.md#localizededges12) | 是 | 相对偏移,组件相对原本的布局位置进行偏移。offset属性不影响父容器布局,仅在绘制时调整位置。<br/>Position类型基于组件自身左上角偏移,Edges类型基于组件自身四边偏移。 offset属性设置 {x: x, y: y} 与设置 {left: x, top: y} 以及 {right: -x, bottom: -y} 效果相同, 类型LocalizedEdges支持镜像模式:LTR模式下start 等同于x,RTL模式下等同于-x<br/>API version 9及以前,默认值为:<br/>{<br/>x: 0,<br/>y: 0<br/>}<br/>默认单位:vp<br/>API version 10:无默认值。 | 98 99## alignRules<sup>9+</sup> 100 101alignRules(value: AlignRuleOption) 102 103指定设置在相对容器中子组件的对齐规则,仅当父容器为[RelativeContainer](ts-container-relativecontainer.md)时生效。 104 105**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 106 107**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 108 109**系统能力:** SystemCapability.ArkUI.ArkUI.Full 110 111**参数:** 112 113| 参数名 | 类型 | 必填 | 说明 | 114| ------ | ------------------------------------------- | ---- | ------------------------ | 115| value | [AlignRuleOption](#alignruleoption对象说明) | 是 | 指定设置在相对容器中子组件的对齐规则。 | 116 117## alignRules<sup>12+</sup> 118 119alignRules(alignRule: LocalizedAlignRuleOptions) 120 121指定设置在相对容器中子组件的对齐规则,仅当父容器为[RelativeContainer](ts-container-relativecontainer.md)时生效。该方法水平方向上以start和end分别替代原方法的left和right,以便在RTL模式下能镜像显示,建议使用该方法指定设置在相对容器中子组件的对齐规则。 122 123**卡片能力:** 从API version 12开始,该接口支持在ArkTS卡片中使用。 124 125**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 126 127**系统能力:** SystemCapability.ArkUI.ArkUI.Full 128 129**参数:** 130 131| 参数名 | 类型 | 必填 | 说明 | 132| ------ | ------------------------------------------- | ---- | ------------------------ | 133| alignRule | [LocalizedAlignRuleOptions](#localizedalignruleoptions12对象说明) | 是 | 指定设置在相对容器中子组件的对齐规则。 | 134 135## AlignRuleOption对象说明 136 137**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 138 139| 名称 | 类型 | 描述 | 140| ------ | ------------------------------------------------------------ | ------------------------------------------------------------ | 141| left | { anchor: string, align: [HorizontalAlign](ts-appendix-enums.md#horizontalalign) } | 设置左对齐参数。<br/>- anchor:设置作为锚点的组件的id值。<br/>- align:设置相对于锚点组件的对齐方式。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 142| right | { anchor: string, align: [HorizontalAlign](ts-appendix-enums.md#horizontalalign) } | 设置右对齐参数。<br/>- anchor:设置作为锚点的组件的id值。<br/>- align:设置相对于锚点组件的对齐方式。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 143| middle | { anchor: string, align: [HorizontalAlign](ts-appendix-enums.md#horizontalalign) } | 设置横向居中对齐方式的参数。<br/>- anchor:设置作为锚点的组件的id值。<br/>- align:设置相对于锚点组件的对齐方式。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 144| top | { anchor: string, align: [VerticalAlign](ts-appendix-enums.md#verticalalign) } | 设置顶部对齐的参数。<br/>- anchor:设置作为锚点的组件的id值。<br/>- align:设置相对于锚点组件的对齐方式。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 145| bottom | { anchor: string, align: [VerticalAlign](ts-appendix-enums.md#verticalalign) } | 设置底部对齐的参数。<br/>- anchor:设置作为锚点的组件的id值。<br/>- align:设置相对于锚点组件的对齐方式。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 146| center | { anchor: string, align: [VerticalAlign](ts-appendix-enums.md#verticalalign) } | 设置纵向居中对齐方式的参数。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 147| bias<sup>11+</sup> | [Bias](#bias对象说明) | 设置组件在锚点约束下的偏移参数,其值为到左/上侧锚点的距离与锚点间总距离的比值。<br/>**卡片能力:** 从API version 11开始,该接口支持在ArkTS卡片中使用。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 148 149## LocalizedAlignRuleOptions<sup>12+</sup>对象说明 150 151**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 152 153| 名称 | 类型 | 描述 | 154| ------ | ------------------------------------------------------------ | ------------------------------------------------------------ | 155| start | [LocalizedHorizontalAlignParam](#localizedhorizontalalignparam12对象说明) | 设置横向对齐方式的参数,LTR模式时为左对齐,RTL模式时为右对齐。 | 156| end | [LocalizedHorizontalAlignParam](#localizedhorizontalalignparam12对象说明) | 设置横向对齐方式的参数,LTR模式时为右对齐,RTL模式时为左对齐。 | 157| middle | [LocalizedHorizontalAlignParam](#localizedhorizontalalignparam12对象说明) | 设置横向居中对齐方式的参数。| 158| top | [LocalizedVerticalAlignParam](#localizedverticalalignparam12对象说明) | 设置纵向顶部对齐的参数。 | 159| bottom | [LocalizedVerticalAlignParam](#localizedverticalalignparam12对象说明) | 设置纵向底部对齐的参数。 | 160| center | [LocalizedVerticalAlignParam](#localizedverticalalignparam12对象说明) | 设置纵向居中对齐方式的参数。 | 161| bias | [Bias](#bias对象说明) | 设置组件在锚点约束下的偏移参数,其值为到左/上侧锚点的距离与锚点间总距离的比值。| 162 163## LocalizedHorizontalAlignParam<sup>12+</sup>对象说明 164 165**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 166 167| 名称 | 类型 | 描述 | 168| ------ | ------------------------------------------------------------ | ------------------------------------------------------------ | 169| anchor | string | 设置作为锚点的组件的id值。 | 170| align | [HorizontalAlign](ts-appendix-enums.md#horizontalalign) | 设置相对于锚点组件的横向对齐方式。 | 171 172## LocalizedVerticalAlignParam<sup>12+</sup>对象说明 173 174**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 175 176| 名称 | 类型 | 描述 | 177| ------ | ------------------------------------------------------------ | ------------------------------------------------------------ | 178| anchor | string | 设置作为锚点的组件的id值。 | 179| align | [VerticalAlign](ts-appendix-enums.md#verticalalign) | 设置相对于锚点组件的纵向对齐方式。 | 180 181## Bias对象说明 182 183设置组件在锚点约束下的偏移参数。 184<br/>以水平方向Bias为例,其值为组件到左锚点的距离 D<sub>start</sub>与组件到水平方向锚点间总距离 D<sub>start</sub> + D<sub>end</sub>的比值。镜像语言下,D<sub>start</sub>为组件到右锚点的距离。下图中D<sub>width</sub>表示组件宽度。 185<br/> 186<br/>竖直方向同理,其值为组件到上锚点的距离D<sub>top</sub>与组件到竖直方向锚点间总距离D<sub>top</sub> + D<sub>bottom</sub>的比值。下图中D<sub>height</sub>表示组件高度。 187<br/> 188 189 190**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 191 192| 参数名 | 类型 | 必填 | 说明 | 193| ----- | ---------------------------------------- | ---- | ---------------------------------------- | 194| horizontal | number | 否 | 水平方向上的bias值。<br/>当子组件的width属性有正确值并且有2个水平方向的锚点时生效。<br/>默认值: 0.5 | 195| vertical | number | 否 | 垂直方向上的bias值。<br/>当子组件的height属性有正确值并且有2个垂直方向的锚点时生效。<br/>默认值: 0.5 | 196 197## chainMode<sup>12+</sup> 198 199chainMode(direction: Axis, style: ChainStyle) 200 201指定以该组件为链头所构成的链的参数,仅当父容器为[RelativeContainer](ts-container-relativecontainer.md)时生效。链头指满足成链规则时链的第一个组件(水平方向从左边起始,镜像语言下从右边起始;竖直方向从上边起始)。 202详细用法请参考[RelativeContainer示例7(设置链)](ts-container-relativecontainer.md#示例7设置链)。 203 204**系统能力:** SystemCapability.ArkUI.ArkUI.Full 205 206**参数:** 207 208| 参数名 | 类型 | 必填 | 说明 | 209| ------ | ------------------------------------------- | ---- | ------------------------ | 210| direction | [Axis](ts-appendix-enums.md#axis) | 是 | 链的方向。 | 211| style | [ChainStyle](#chainstyle12) | 是 | 链的样式。 | 212 213## ChainStyle<sup>12+</sup> 214 215定义链的风格。 216 217**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 218 219**系统能力:** SystemCapability.ArkUI.ArkUI.Full 220 221| 名称 | 说明 | 222| ------------- | ------------------------------------------------------------ | 223| SPREAD | 组件在约束锚点间均匀分布。详细用法请参考[RelativeContainer示例7(设置链)](ts-container-relativecontainer.md#示例7设置链)。 | 224| SPREAD_INSIDE | 除首尾2个子组件的其他组件在约束锚点间均匀分布。详细用法请参考[RelativeContainer示例7(设置链)](ts-container-relativecontainer.md#示例7设置链)。 | 225| PACKED | 链内子组件无间隙。详细用法请参考[RelativeContainer示例7(设置链)](ts-container-relativecontainer.md#示例7设置链)。 | 226 227## chainWeight<sup>14+</sup> 228 229chainWeight(chainWeight: ChainWeightOptions) 230 231对形成链的组件进行重新布局。仅当父容器为[RelativeContainer](ts-container-relativecontainer.md)时生效。 232 233**系统能力:** SystemCapability.ArkUI.ArkUI.Full 234 235**原子化服务API:** 从API version 14开始,该接口支持在原子化服务中使用。 236 237**参数:** 238 239| 参数名 | 类型 | 必填 | 说明 | 240| ------ | ------------------------------------------- | ---- | ------------------------ | 241| chainWeight | [ChainWeightOptions](ts-types.md#chainweightoptions14对象说明) | 是 | 设置了chainWeight属性的组件与同一条链上的兄弟组件在水平或竖直方向的尺寸会按照设置的权重进行分配,分配时会忽略组件本身尺寸设置,按分配的权重自适应占满剩余空间。 | 242 243## 示例 244 245### 示例1(对齐方式和主轴方向上的布局) 246 247设置内容在元素内的对齐方式和子元素在父容器主轴方向上的布局。 248 249```ts 250// xxx.ets 251@Entry 252@Component 253struct PositionExample1 { 254 build() { 255 Column() { 256 Column({ space: 10 }) { 257 // 元素内容<元素宽高,设置内容在与元素内的对齐方式 258 Text('align').fontSize(9).fontColor(0xCCCCCC).width('90%') 259 Stack() { 260 Text('First show in bottom end').height('65%').backgroundColor(0xD2B48C) 261 Text('Second show in bottom end').backgroundColor(0xF5DEB3).opacity(0.9) 262 }.width('90%').height(50).margin({ top: 5 }).backgroundColor(0xFFE4C4) 263 .align(Alignment.BottomEnd) 264 Stack() { 265 Text('top start') 266 }.width('90%').height(50).margin({ top: 5 }).backgroundColor(0xFFE4C4) 267 .align(Alignment.TopStart) 268 269 // 父容器设置direction为Direction.Ltr,子元素从左到右排列 270 Text('direction').fontSize(9).fontColor(0xCCCCCC).width('90%') 271 Row() { 272 Text('1').height(50).width('25%').fontSize(16).backgroundColor(0xF5DEB3) 273 Text('2').height(50).width('25%').fontSize(16).backgroundColor(0xD2B48C) 274 Text('3').height(50).width('25%').fontSize(16).backgroundColor(0xF5DEB3) 275 Text('4').height(50).width('25%').fontSize(16).backgroundColor(0xD2B48C) 276 } 277 .width('90%') 278 .direction(Direction.Ltr) 279 // 父容器设置direction为Direction.Rtl,子元素从右到左排列 280 Row() { 281 Text('1').height(50).width('25%').fontSize(16).backgroundColor(0xF5DEB3).textAlign(TextAlign.End) 282 Text('2').height(50).width('25%').fontSize(16).backgroundColor(0xD2B48C).textAlign(TextAlign.End) 283 Text('3').height(50).width('25%').fontSize(16).backgroundColor(0xF5DEB3).textAlign(TextAlign.End) 284 Text('4').height(50).width('25%').fontSize(16).backgroundColor(0xD2B48C).textAlign(TextAlign.End) 285 } 286 .width('90%') 287 .direction(Direction.Rtl) 288 } 289 } 290 .width('100%').margin({ top: 5 }) 291 } 292} 293``` 294 295 296 297### 示例2(位置偏移) 298 299基于父组件、相对定位、锚点作出位置偏移。 300 301```ts 302// xxx.ets 303@Entry 304@Component 305struct PositionExample2 { 306 build() { 307 Column({ space: 20 }) { 308 // 设置子组件左上角相对于父组件左上角的偏移位置 309 Text('position').fontSize(12).fontColor(0xCCCCCC).width('90%') 310 Row() { 311 Text('1').size({ width: '30%', height: '50' }).backgroundColor(0xdeb887).border({ width: 1 }).fontSize(16) 312 .textAlign(TextAlign.Center) 313 Text('2 position(30, 10)') 314 .size({ width: '60%', height: '30' }) 315 .backgroundColor(0xbbb2cb) 316 .border({ width: 1 }) 317 .fontSize(16) 318 .align(Alignment.Start) 319 .position({ x: 30, y: 10 }) 320 Text('3').size({ width: '45%', height: '50' }).backgroundColor(0xdeb887).border({ width: 1 }).fontSize(16) 321 .textAlign(TextAlign.Center) 322 Text('4 position(50%, 70%)') 323 .size({ width: '50%', height: '50' }) 324 .backgroundColor(0xbbb2cb) 325 .border({ width: 1 }) 326 .fontSize(16) 327 .position({ x: '50%', y: '70%' }) 328 }.width('90%').height(100).border({ width: 1, style: BorderStyle.Dashed }) 329 330 // 相对于起点偏移,其中x为最终定位点距离起点水平方向间距,x>0往左,反之向右。 331 // y为最终定位点距离起点垂直方向间距,y>0向上,反之向下 332 Text('markAnchor').fontSize(12).fontColor(0xCCCCCC).width('90%') 333 Stack({ alignContent: Alignment.TopStart }) { 334 Row() 335 .size({ width: '100', height: '100' }) 336 .backgroundColor(0xdeb887) 337 Text('text') 338 .fontSize('30px') 339 .textAlign(TextAlign.Center) 340 .size({ width: 25, height: 25 }) 341 .backgroundColor(Color.Green) 342 .markAnchor({ x: 25, y: 25 }) 343 Text('text') 344 .fontSize('30px') 345 .textAlign(TextAlign.Center) 346 .size({ width: 25, height: 25 }) 347 .backgroundColor(Color.Green) 348 .markAnchor({ x: -100, y: -25 }) 349 Text('text') 350 .fontSize('30px') 351 .textAlign(TextAlign.Center) 352 .size({ width: 25, height: 25 }) 353 .backgroundColor(Color.Green) 354 .markAnchor({ x: 25, y: -25 }) 355 }.margin({ top: 25 }).border({ width: 1, style: BorderStyle.Dashed }) 356 357 // 相对定位,x>0向右偏移,反之向左,y>0向下偏移,反之向上 358 Text('offset').fontSize(12).fontColor(0xCCCCCC).width('90%') 359 Row() { 360 Text('1').size({ width: '15%', height: '50' }).backgroundColor(0xdeb887).border({ width: 1 }).fontSize(16) 361 .textAlign(TextAlign.Center) 362 Text('2 offset(15, 30)') 363 .size({ width: 120, height: '50' }) 364 .backgroundColor(0xbbb2cb) 365 .border({ width: 1 }) 366 .fontSize(16) 367 .align(Alignment.Start) 368 .offset({ x: 15, y: 30 }) 369 Text('3').size({ width: '15%', height: '50' }).backgroundColor(0xdeb887).border({ width: 1 }).fontSize(16) 370 .textAlign(TextAlign.Center) 371 Text('4 offset(-5%, 20%)') 372 .size({ width: 100, height: '50' }) 373 .backgroundColor(0xbbb2cb) 374 .border({ width: 1 }) 375 .fontSize(16) 376 .offset({ x: '-5%', y: '20%' }) 377 }.width('90%').height(100).border({ width: 1, style: BorderStyle.Dashed }) 378 } 379 .width('100%').margin({ top: 25 }) 380 } 381} 382``` 383 384 385 386### 示例3(绝对定位和相对偏移) 387 388使用position设置绝对定位,确定子组件相对父组件的位置。使用offset设置相对偏移,组件相对原本的布局位置进行偏移。 389 390```ts 391// xxx.ets 392@Entry 393@Component 394struct Example3 { 395 build() { 396 Column({ space: 20 }){ 397 Text('position use Edges').fontSize(12).fontColor(0xCCCCCC).width('90%') 398 Row() { 399 Text('bottom:0, right:0').size({ width: '30%', height: '50' }).backgroundColor(0xdeb887).border({ width: 1 }).fontSize(16) 400 .textAlign(TextAlign.Center).position({bottom: 0, right: 0}) 401 Text('top:0, left:0').size({ width: '30%', height: '50' }).backgroundColor(0xdeb887).border({ width: 1 }).fontSize(16) 402 .textAlign(TextAlign.Center).position({top: 0, left: 0}) 403 Text('top:10%, left:50%').size({ width: '50%', height: '30' }).backgroundColor(0xbbb2cb).border({ width: 1 }).fontSize(16) 404 .textAlign(TextAlign.Center).position({ top: '10%', left: '50%' }) 405 Text('bottom:0, left:30').size({ width: '50%', height: '30' }).backgroundColor(0xbbb2cb).border({ width: 1 }).fontSize(16) 406 .textAlign(TextAlign.Center).position({ bottom: 0, left: 30 }) 407 }.width('90%').height(100).border({ width: 1, style: BorderStyle.Dashed }) 408 409 410 Text('offset use Edges').fontSize(12).fontColor(0xCCCCCC).width('90%') 411 Row() { 412 Text('1').size({ width: '25%', height: 50 }).backgroundColor(0xdeb887).border({ width: 1 }).fontSize(16) 413 .textAlign(TextAlign.Center) 414 Text('2 top:30, left:0').size({ width: '25%', height: 50 }).backgroundColor(0xbbb2cb).border({ width: 1 }).fontSize(16) 415 .textAlign(TextAlign.Center).offset({top: 30, left: 0}) 416 Text('3').size({ width: '25%', height: 50 }).backgroundColor(0xdeb887).border({ width: 1 }).fontSize(16) 417 .textAlign(TextAlign.Center) 418 Text('4 bottom:10, right:30').size({ width: '25%', height: 50 }).backgroundColor(0xbbb2cb).border({ width: 1 }).fontSize(12) 419 .textAlign(TextAlign.Center).offset({bottom: 10, right: 30}) 420 }.width('90%').height(150).border({ width: 1, style: BorderStyle.Dashed }) 421 }.width('100%').margin({ top: 25 }) 422 } 423} 424``` 425 426 427 428### 示例4(镜像效果) 429 430通用布局属性支持镜像能力。从上到下依次通过position,offset,markAnchor实现镜像效果。黄色赋值为原本效果,粉色赋值为镜像效果。 431 432```ts 433// xxx.ets 434import { LengthMetrics } from '@kit.ArkUI'; 435@Entry 436@Component 437struct Example4 { 438 private scroller: Scroller = new Scroller() 439 440 build() { 441 Column() { 442 Stack({ alignContent: Alignment.End }) { 443 Scroll(this.scroller) { 444 Flex({ direction: FlexDirection.Column }) { 445 RelativeContainer() { 446 Row() { 447 } 448 .position({ start: LengthMetrics.px(200), top: LengthMetrics.px(100) }) 449 .width("30%") 450 .height("20%") 451 .backgroundColor(Color.Pink) 452 .padding(50) 453 .margin(50) 454 Row() { 455 } 456 .position({ left:'200px', top: '100px' }) 457 .width("30%") 458 .height("20%") 459 .backgroundColor(Color.Yellow) 460 .padding(50) 461 .margin(50) 462 Row() { 463 } 464 .offset({ start: LengthMetrics.vp(100), top: LengthMetrics.vp(200) }) 465 .width("30%") 466 .height("20%") 467 .backgroundColor(Color.Pink) 468 .padding(50) 469 .margin(50) 470 Row() { 471 } 472 .offset({ left: 100, top: 200 }) 473 .width("30%") 474 .height("20%") 475 .backgroundColor(Color.Yellow) 476 .padding(50) 477 .margin(50) 478 Row() { 479 } 480 .markAnchor({ start: LengthMetrics.fp(100), top: LengthMetrics.fp(-350) }) 481 .width("30%") 482 .height("20%") 483 .backgroundColor(Color.Pink) 484 .padding(50) 485 .margin(50) 486 Row() { 487 } 488 .markAnchor({ x: '100fp', y: '-350fp' }) 489 .width("30%") 490 .height("20%") 491 .backgroundColor(Color.Yellow) 492 .padding(50) 493 .margin(50) 494 } 495 .backgroundColor(Color.White) 496 .padding(50) 497 .margin(50) 498 } 499 } 500 .width('100%') 501 .scrollBar(BarState.Off) 502 .scrollable(ScrollDirection.Vertical) 503 504 ScrollBar({ scroller: this.scroller, direction: ScrollBarDirection.Vertical, state: BarState.Auto }) { 505 Text() 506 .width(20) 507 .height(100) 508 .borderRadius(10) 509 .backgroundColor('#C0C0C0') 510 }.width(20).backgroundColor('#ededed') 511 } 512 }.height('90%') 513 } 514} 515``` 516