ts-universal-attributes-location.md 8.5 KB
Newer Older
Z
zengyawen 已提交
1
# Location
Z
zengyawen 已提交
2

E
ester.zhou 已提交
3
The location attributes set the alignment mode, layout direction, and position of a component.
Z
zengyawen 已提交
4

5 6
>  **NOTE**
>
E
ester.zhou 已提交
7
>  The APIs of this module are supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
8 9 10 11 12


## Attributes


E
ester.zhou 已提交
13 14
| Name| Type| Description|
| -------- | -------- | -------- |
E
ester.zhou 已提交
15 16 17 18 19 20
| align | [Alignment](ts-appendix-enums.md#alignment) | Alignment mode of the component content in the drawing area.<br>Default value: **Alignment.Center**<br>Since API version 9, this API is supported in ArkTS widgets.|
| direction | [Direction](ts-appendix-enums.md#direction) | Horizontal layout of the component.<br>Default value: **Direction.Auto**<br>Since API version 9, this API is supported in ArkTS widgets.|
| position | [Position](ts-types.md#position8) | Offset of the component's upper left corner relative to the parent component's upper left corner. This offset is expressed using absolute values. When laying out components, this attribute does not affect the layout of the parent component. It only adjusts the component position during drawing.<br>Since API version 9, this API is supported in ArkTS widgets.|
| markAnchor | [Position](ts-types.md#position8) | Anchor point of the component for positioning. The upper left corner of the component is used as the reference point for offset. Generally, this attribute is used together with the **position** and **offset** attributes. When used independently, this attribute is similar to **offset**.<br>Default value:<br>{<br>x: 0,<br>y: 0<br>}<br>Since API version 9, this API is supported in ArkTS widgets.|
| offset | [Position](ts-types.md#position8) | Offset of the component relative to itself. This offset is expressed using relative values. This attribute does not affect the layout of the parent component. It only adjusts the component position during drawing.<br>Default value:<br>{<br>x: 0,<br>y: 0<br>}<br>Since API version 9, this API is supported in ArkTS widgets.|
| 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>} | Alignment rules relative to the container. This attribute is valid only when the container is [\<RelativeContainer>](ts-container-relativecontainer.md).<br>- **left**: left-aligned.<br>- **right**: right-aligned.<br>- **middle**: center-aligned.<br>- **top**: top-aligned.<br>- **bottom**: bottom-aligned.<br>- **center**: center-aligned.<br>This API is supported in ArkTS widgets.<br>**NOTE**<br>- **anchor**: ID of the component that functions as the anchor point.<br>- **align**: alignment mode relative to the anchor component.|
Z
zengyawen 已提交
21 22 23


## Example
E
ester.zhou 已提交
24
### Example 1
25 26
```ts
// xxx.ets
Z
zengyawen 已提交
27 28
@Entry
@Component
E
ester.zhou 已提交
29
struct PositionExample1 {
Z
zengyawen 已提交
30 31
  build() {
    Column() {
E
ester.zhou 已提交
32 33
      Column({ space: 10 }) {
        // When the component content is within the area specified by the component width and height, set the alignment mode of the content in the component.
Z
zengyawen 已提交
34 35 36 37 38 39 40 41
        Text('align').fontSize(9).fontColor(0xCCCCCC).width('90%')
        Text('top start')
          .align(Alignment.TopStart)
          .height(50)
          .width('90%')
          .fontSize(16)
          .backgroundColor(0xFFE4C4)

E
ester.zhou 已提交
42 43 44 45 46 47 48
        Text('Bottom end')
          .align(Alignment.BottomEnd)
          .height(50)
          .width('90%')
          .fontSize(16)
          .backgroundColor(0xFFE4C4)

E
ester.zhou 已提交
49
        // To arrange the child components from left to right, set direction of the parent container to Direction.Ltr.
Z
zengyawen 已提交
50 51 52 53 54 55 56 57
        Text('direction').fontSize(9).fontColor(0xCCCCCC).width('90%')
        Row() {
          Text('1').height(50).width('25%').fontSize(16).backgroundColor(0xF5DEB3)
          Text('2').height(50).width('25%').fontSize(16).backgroundColor(0xD2B48C)
          Text('3').height(50).width('25%').fontSize(16).backgroundColor(0xF5DEB3)
          Text('4').height(50).width('25%').fontSize(16).backgroundColor(0xD2B48C)
        }
        .width('90%')
E
ester.zhou 已提交
58
        .direction(Direction.Ltr)
E
ester.zhou 已提交
59 60
        // To arrange the child components from right to left, set direction of the parent container to Direction.Rtl.
        Row() {
E
ester.zhou 已提交
61 62 63 64
          Text('1').height(50).width('25%').fontSize(16).backgroundColor(0xF5DEB3).textAlign(TextAlign.End)
          Text('2').height(50).width('25%').fontSize(16).backgroundColor(0xD2B48C).textAlign(TextAlign.End)
          Text('3').height(50).width('25%').fontSize(16).backgroundColor(0xF5DEB3).textAlign(TextAlign.End)
          Text('4').height(50).width('25%').fontSize(16).backgroundColor(0xD2B48C).textAlign(TextAlign.End)
E
ester.zhou 已提交
65 66
        }
        .width('90%')
Z
zengyawen 已提交
67 68 69
        .direction(Direction.Rtl)
      }
    }
E
ester.zhou 已提交
70
    .width('100%').margin({ top: 5 })
Z
zengyawen 已提交
71 72 73 74
  }
}
```

E
ester.zhou 已提交
75
![align.png](figures/align.png)
Z
zengyawen 已提交
76

E
ester.zhou 已提交
77
### Example 2
78 79
```ts
// xxx.ets
Z
zengyawen 已提交
80 81 82 83 84
@Entry
@Component
struct PositionExample2 {
  build() {
    Column({ space: 20 }) {
E
ester.zhou 已提交
85
      // Set the offset of the component's upper left corner relative to the parent component's upper left corner.
Z
zengyawen 已提交
86
      Text('position').fontSize(12).fontColor(0xCCCCCC).width('90%')
E
ester.zhou 已提交
87 88 89 90 91 92 93 94 95
      Row() {
        Text('1').size({ width: '30%', height: '50' }).backgroundColor(0xdeb887).border({ width: 1 }).fontSize(16)
        Text('2 position(30, 10)')
          .size({ width: '60%', height: '30' })
          .backgroundColor(0xbbb2cb)
          .border({ width: 1 })
          .fontSize(16)
          .align(Alignment.Start)
          .position({ x: 30, y: 10 })
Z
zengyawen 已提交
96 97
        Text('3').size({ width: '45%', height: '50' }).backgroundColor(0xdeb887).border({ width: 1 }).fontSize(16)
        Text('4 position(50%, 70%)')
E
ester.zhou 已提交
98 99 100 101
          .size({ width: '50%', height: '50' })
          .backgroundColor(0xbbb2cb)
          .border({ width: 1 })
          .fontSize(16)
Z
zengyawen 已提交
102 103 104
          .position({ x: '50%', y: '70%' })
      }.width('90%').height(100).border({ width: 1, style: BorderStyle.Dashed })

E
ester.zhou 已提交
105 106
      // Offset relative to the start point. x indicates the horizontal distance between the end point and the start point. If the value of x is greater than 0, the component is offset to the left. Otherwise, the component is offset to the right.
      // y indicates the vertical distance between the end point and the start point. If the value of y is greater than 0, the component is offset to the top. Otherwise, the component is offset to the bottom.
Z
zengyawen 已提交
107 108 109 110 111
      Text('markAnchor').fontSize(12).fontColor(0xCCCCCC).width('90%')
      Stack({ alignContent: Alignment.TopStart }) {
        Row()
          .size({ width: '100', height: '100' })
          .backgroundColor(0xdeb887)
E
ester.zhou 已提交
112
        Text('text')
Z
zengyawen 已提交
113
          .size({ width: 25, height: 25 })
E
ester.zhou 已提交
114
          .backgroundColor(Color.Green)
Z
zengyawen 已提交
115
          .markAnchor({ x: 25, y: 25 })
E
ester.zhou 已提交
116
        Text('text')
Z
zengyawen 已提交
117
          .size({ width: 25, height: 25 })
E
ester.zhou 已提交
118 119 120 121 122 123
          .backgroundColor(Color.Green)
          .markAnchor({ x: -100, y: -25 })
        Text('text')
          .size({ width: 25, height: 25 })
          .backgroundColor(Color.Green)
          .markAnchor({ x: 25, y: -25 })
Z
zengyawen 已提交
124 125
      }.margin({ top: 25 }).border({ width: 1, style: BorderStyle.Dashed })

E
ester.zhou 已提交
126
      // Offset of the component relative to itself. If the value of x is greater than 0, the component is offset to the right. Otherwise, the component is offset to the left. If the value of y is greater than 0, the component is offset to the bottom. Otherwise, the component is offset to the top.
Z
zengyawen 已提交
127 128 129
      Text('offset').fontSize(12).fontColor(0xCCCCCC).width('90%')
      Row() {
        Text('1').size({ width: '15%', height: '50' }).backgroundColor(0xdeb887).border({ width: 1 }).fontSize(16)
E
ester.zhou 已提交
130 131 132 133 134 135 136
        Text('2  offset(15, 30)')
          .size({ width: 120, height: '50' })
          .backgroundColor(0xbbb2cb)
          .border({ width: 1 })
          .fontSize(16)
          .align(Alignment.Start)
          .offset({ x: 15, y: 30 })
Z
zengyawen 已提交
137
        Text('3').size({ width: '15%', height: '50' }).backgroundColor(0xdeb887).border({ width: 1 }).fontSize(16)
E
ester.zhou 已提交
138 139 140 141 142 143
        Text('4 offset(-10%, 20%)')
          .size({ width: 100, height: '50' })
          .backgroundColor(0xbbb2cb)
          .border({ width: 1 })
          .fontSize(16)
          .offset({ x: '-5%', y: '20%' })
Z
zengyawen 已提交
144 145 146 147 148 149 150
      }.width('90%').height(100).border({ width: 1, style: BorderStyle.Dashed })
    }
    .width('100%').margin({ top: 25 })
  }
}
```

E
ester.zhou 已提交
151
![position.png](figures/position.png)