1# 位置设置 2 3设置组件的对齐方式、布局方向和显示位置。 4 5> **说明:** 6> 7> 从API Version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。 8 9 10## 属性 11 12 13| 名称 | 参数类型 | 描述 | 14| -------- | -------- | -------- | 15| align | [Alignment](ts-appendix-enums.md#alignment) | 设置元素内容的对齐方式,当元素的width和height大小大于元素本身内容大小时生效。<br/>默认值:Alignment.Center | 16| direction | [Direction](ts-appendix-enums.md#direction) | 设置元素水平方向的布局。<br/>默认值:Direction.Auto | 17| position | [Position](ts-types.md#position8) | 绝对定位,设置元素左上角相对于父容器左上角偏移位置。在布局容器中,设置该属性不影响父容器布局,仅在绘制时进行位置调整。 | 18| markAnchor | [Position](ts-types.md#position8) | 设置元素在位置定位时的锚点,以元素左上角作为基准点进行偏移。通常配合position和offset属性使用,单独使用时,效果类似offset<br/>默认值:<br/>{<br/>x: 0,<br/>y: 0<br/>} | 19| offset | [Position](ts-types.md#position8) | 相对定位,设置元素相对于自身的偏移量。设置该属性,不影响父容器布局,仅在绘制时进行位置调整。<br/>默认值:<br/>{<br/>x: 0,<br/>y: 0<br/>} | 20| alignRules<sup>9+</sup> | {<br/>left?: { anchor: string, align: [HorizontalAlign](ts-appendix-enums.md#horizontalalign) };<br/>right?: { anchor: string, align: [HorizontalAlign](ts-appendix-enums.md#horizontalalign) };<br/>middle?: { anchor: string, align: [HorizontalAlign](ts-appendix-enums.md#horizontalalign) };<br/>top?: { anchor: string, align: [VerticalAlign](ts-appendix-enums.md#verticalalign) };<br/>bottom?: { anchor: string, align: [VerticalAlign](ts-appendix-enums.md#verticalalign) };<br/>center?: { anchor: string, align: [VerticalAlign](ts-appendix-enums.md#verticalalign) }<br/>} | 指定相对容器的对齐规则。<br/>- left:设置左对齐参数。<br/>- right:设置右对齐参数。<br/>- middle:设置中间对齐的参数。<br/>- top:设置顶部对齐的参数。<br/>- bottom:设置底部对齐的参数。<br/>- center:设置中心对齐的参数。<br/>**说明:**<br/>- anchor:设置作为锚点的组件的id值。<br>- align:设置相对于锚点组件的对齐方式。 | 21 22 23## 示例 24### 示例1 25```ts 26// xxx.ets 27@Entry 28@Component 29struct PositionExample1 { 30 build() { 31 Column() { 32 Column({ space: 10 }) { 33 // 元素内容<元素宽高,设置内容在与元素内的对齐方式 34 Text('align').fontSize(9).fontColor(0xCCCCCC).width('90%') 35 Text('top start') 36 .align(Alignment.TopStart) 37 .height(50) 38 .width('90%') 39 .fontSize(16) 40 .backgroundColor(0xFFE4C4) 41 42 Text('Bottom end') 43 .align(Alignment.BottomEnd) 44 .height(50) 45 .width('90%') 46 .fontSize(16) 47 .backgroundColor(0xFFE4C4) 48 49 // 父容器设置direction为Direction.Auto|Ltr|不设置,子元素从左到右排列 50 Text('direction').fontSize(9).fontColor(0xCCCCCC).width('90%') 51 Row() { 52 Text('1').height(50).width('25%').fontSize(16).backgroundColor(0xF5DEB3) 53 Text('2').height(50).width('25%').fontSize(16).backgroundColor(0xD2B48C) 54 Text('3').height(50).width('25%').fontSize(16).backgroundColor(0xF5DEB3) 55 Text('4').height(50).width('25%').fontSize(16).backgroundColor(0xD2B48C) 56 } 57 .width('90%') 58 .direction(Direction.Auto) 59 // 父容器设置direction为Direction.Rtl,子元素从右到左排列 60 Row() { 61 Text('1').height(50).width('25%').fontSize(16).backgroundColor(0xF5DEB3) 62 Text('2').height(50).width('25%').fontSize(16).backgroundColor(0xD2B48C) 63 Text('3').height(50).width('25%').fontSize(16).backgroundColor(0xF5DEB3) 64 Text('4').height(50).width('25%').fontSize(16).backgroundColor(0xD2B48C) 65 } 66 .width('90%') 67 .direction(Direction.Rtl) 68 } 69 } 70 .width('100%').margin({ top: 5 }).direction(Direction.Rtl) 71 } 72} 73``` 74 75 76 77### 示例2 78```ts 79// xxx.ets 80@Entry 81@Component 82struct PositionExample2 { 83 build() { 84 Column({ space: 20 }) { 85 // 设置子组件左上角相对于父组件左上角的偏移位置 86 Text('position').fontSize(12).fontColor(0xCCCCCC).width('90%') 87 Row() { 88 Text('1').size({ width: '30%', height: '50' }).backgroundColor(0xdeb887).border({ width: 1 }).fontSize(16) 89 Text('2 position(30, 10)') 90 .size({ width: '60%', height: '30' }) 91 .backgroundColor(0xbbb2cb) 92 .border({ width: 1 }) 93 .fontSize(16) 94 .align(Alignment.Start) 95 .position({ x: 30, y: 10 }) 96 Text('3').size({ width: '45%', height: '50' }).backgroundColor(0xdeb887).border({ width: 1 }).fontSize(16) 97 Text('4 position(50%, 70%)') 98 .size({ width: '50%', height: '50' }) 99 .backgroundColor(0xbbb2cb) 100 .border({ width: 1 }) 101 .fontSize(16) 102 .position({ x: '50%', y: '70%' }) 103 }.width('90%').height(100).border({ width: 1, style: BorderStyle.Dashed }) 104 105 // 相对于起点偏移,其中x为最终定位点距离起点水平方向间距,x>0往左,反之向右。 106 // y为最终定位点距离起点垂直方向间距,y>0向上,反之向下 107 Text('markAnchor').fontSize(12).fontColor(0xCCCCCC).width('90%') 108 Stack({ alignContent: Alignment.TopStart }) { 109 Row() 110 .size({ width: '100', height: '100' }) 111 .backgroundColor(0xdeb887) 112 Text('text') 113 .size({ width: 25, height: 25 }) 114 .backgroundColor(Color.Green) 115 .markAnchor({ x: 25, y: 25 }) 116 Text('text') 117 .size({ width: 25, height: 25 }) 118 .backgroundColor(Color.Green) 119 .markAnchor({ x: -100, y: -25 }) 120 Text('text') 121 .size({ width: 25, height: 25 }) 122 .backgroundColor(Color.Green) 123 .markAnchor({ x: 25, y: -25 }) 124 }.margin({ top: 25 }).border({ width: 1, style: BorderStyle.Dashed }) 125 126 // 相对定位,x>0向右偏移,反之向左,y>0向下偏移,反之向上 127 Text('offset').fontSize(12).fontColor(0xCCCCCC).width('90%') 128 Row() { 129 Text('1').size({ width: '15%', height: '50' }).backgroundColor(0xdeb887).border({ width: 1 }).fontSize(16) 130 Text('2 offset(15, 30)') 131 .size({ width: 120, height: '50' }) 132 .backgroundColor(0xbbb2cb) 133 .border({ width: 1 }) 134 .fontSize(16) 135 .align(Alignment.Start) 136 .offset({ x: 15, y: 30 }) 137 Text('3').size({ width: '15%', height: '50' }).backgroundColor(0xdeb887).border({ width: 1 }).fontSize(16) 138 Text('4 offset(-10%, 20%)') 139 .size({ width: 100, height: '50' }) 140 .backgroundColor(0xbbb2cb) 141 .border({ width: 1 }) 142 .fontSize(16) 143 .offset({ x: '-5%', y: '20%' }) 144 }.width('90%').height(100).border({ width: 1, style: BorderStyle.Dashed }) 145 } 146 .width('100%').margin({ top: 25 }) 147 } 148} 149``` 150 151 152