• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Relative Layout (RelativeContainer)
2
3
4## Overview
5
6The relative layout, implemented using the [\<RelativeContainer>](../reference/arkui-ts/ts-container-relativecontainer.md) container component, is used to lay out child elements in relative positions. A child element can set the container or another child element as the anchor, based on which its relative position is determined. Below shows a relative layout. The dotted lines in the figure indicate the position dependency.
7
8
9  **Figure 1** Relative layout
10
11![relative-layout](figures/relative-layout.png)
12
13
14A child element does not necessarily adopt the dependency shown above to determine its relative position. For example, Item4 may use Item2 or the **\<RelativeContainer>** parent container as a dependency anchor.
15
16
17## Basic Concepts
18
19- Anchor: element relative to which an element's position is specified.
20
21- Alignment mode: how the current element is aligned with the anchor, which can be top-, center-, or bottom-aligned in the vertical direction or left-, center-, and right-aligned in the horizontal direction.
22
23
24## Setting the Dependency
25
26
27### Setting the Anchor
28
29By setting the anchor, you set a position dependency relationship between a child element and its parent element or sibling elements. In the horizontal direction, you can set the left, middle, and right anchors. In the vertical direction, you can set the top, center, and bottom anchors. To specify anchors, you must set IDs for the **\<RelativeContainer>** component and its child elements. The default ID is **__container__**. The ID is set through the **id** attribute. Child elements whose IDs are not set are not displayed in the **\<RelativeContainer>** component.
30
31>**NOTE**
32>
33>When using anchors, pay attention to the relative positions of child elements to avoid misplacement or blocking.
34
35- The ID of the **\<RelativeContainer>** parent component is **__container__**.
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    'left': { 'anchor': '__container__', 'align': HorizontalAlign.End }
45  }
46  RelativeContainer() {
47    Row()
48      // Add other attributes.
49      .alignRules(AlignRus)
50      .id("row1")
51
52    Row()
53      ...
54      .alignRules(AlignRue)
55      .id("row2")
56  }
57  ```
58
59  ![en-us_image_0000001562820901](figures/en-us_image_0000001562820901.png)
60
61- A child element is used as the anchor.
62
63  ```ts
64  let RelConB:Record<string,Record<string,string|VerticalAlign|HorizontalAlign>> = {
65    'top': { 'anchor': 'row1', 'align': VerticalAlign.Bottom }
66  }
67  let Mleft:Record<string,number> = { 'left': 20 }
68  let BWC:Record<string,number|string> = { 'width': 2, 'color': '#6699FF' }
69  RelativeContainer() {RelConB}
70  .width(300).height(300)
71  .margin(Mleft)
72  .border(BWC)
73  ```
74
75  ![en-us_image_0000001562940613](figures/en-us_image_0000001562940613.png)
76
77
78### Setting Alignment Relative to the Anchor
79
80After an anchor is set, you can use **align** to set the alignment mode relative to the anchor.
81
82Alignment modes in the horizontal direction can be left, center, or right, achieved by the **HorizontalAlign.Start**, **HorizontalAlign.Center**, and **HorizontalAlign.End** attributes, respectively.
83
84![alignment-relative-anchor-horizontal](figures/alignment-relative-anchor-horizontal.png)
85
86Alignment modes in the vertical direction can be top, center, or bottom, achieved by the **VerticalAlign.Top**, **VerticalAlign.Center**, and **VerticalAlign.Bottom** attributes, respectively.
87
88![alignment-relative-anchor-vertical](figures/alignment-relative-anchor-vertical.png)
89
90
91## Example
92
93Child elements in the relative layout are flexible. You can use **alignRules** to move child elements in the **\<RelativeContainer>** container.
94
95
96```ts
97@Entry
98@Component
99struct Index {
100  build() {
101    Row() {
102      RelativeContainer() {
103        Row()
104          .width(100)
105          .height(100)
106          .backgroundColor('#FF3333')
107          .alignRules({
108            top: { anchor: '__container__', align: VerticalAlign.Top },  // Use the parent container as the anchor and align with its top vertically.
109            middle: { anchor: '__container__', align: HorizontalAlign.Center }  // Use the parent container as the anchor and align with its center horizontally.
110          })
111          .id('row1') // Set the anchor to row1.
112
113        Row() {
114          Image($r('app.media.icon'))
115        }
116        .height(100).width(100)
117        .alignRules({
118          top: { anchor: 'row1', align: VerticalAlign.Bottom },  // Use row1 as the anchor and align with its bottom vertically.
119          left: { anchor: 'row1', align: HorizontalAlign.Start }  // Use row1 as the anchor and align with its start edge horizontally.
120        })
121        .id('row2') // Set the anchor to row2.
122
123        Row()
124          .width(100)
125          .height(100)
126          .backgroundColor('#FFCC00')
127          .alignRules({
128            top: { anchor: 'row2', align: VerticalAlign.Top }
129          })
130          .id('row3') // Set the anchor to row3.
131
132        Row()
133          .width(100)
134          .height(100)
135          .backgroundColor('#FF9966')
136          .alignRules({
137            top: { anchor: 'row2', align: VerticalAlign.Top },
138            left: { anchor: 'row2', align: HorizontalAlign.End },
139          })
140          .id('row4') // Set the anchor to row4.
141
142        Row()
143          .width(100)
144          .height(100)
145          .backgroundColor('#FF66FF')
146          .alignRules({
147            top: { anchor: 'row2', align: VerticalAlign.Bottom },
148            middle: { anchor: 'row2', align: HorizontalAlign.Center }
149          })
150          .id('row5') // Set the anchor to row5.
151      }
152      .width(300).height(300)
153      .border({ width: 2, color: '#6699FF' })
154    }
155    .height('100%').margin({ left: 30 })
156  }
157}
158```
159
160![en-us_image_0000001562700529](figures/en-us_image_0000001562700529.png)
161