We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.

Only Essential Cookies
Accept All
ReferencesApplication FrameworkArkUIArkTS ComponentsRow, Column, and StackRelativeContainer

RelativeContainer

Phone12+PC/2in113+Tablet12+TV19+Wearable18+

The RelativeContainer component is a container component used for relative layout of elements in complex scenarios.

Child components can define their alignment rules within the container using alignRules.

NOTE
  • This component is supported since API version 9. Updates will be marked with a superscript to indicate their earliest API version.
  • When width and height are not set, RelativeContainer defaults to 100% in both dimensions.
  • Since API version 11, setting width or height to "auto" enables child-adaptive sizing. However, if the child components use the container as an anchor in the horizontal direction, the auto value of width has no effect (equivalent to width not being set). The same rule applies to the vertical direction.
  • Since API version 20, the size adaptation behavior of child components in the RelativeContainer component follows the following rules, depending on the LayoutPolicy setting for width and height: LayoutPolicy.wrapContent: The child component adapts to its content size and is constrained by the size of the ancestor node. LayoutPolicy.fixAtIdealSize: The child component adapts to its ideal content size and is not constrained by the size of the ancestor node. If width is set to wrapContent or fixAtIdealSize, and the child component (in the horizontal direction) directly or indirectly uses the RelativeContainer as its anchor, the container's horizontal size will not adapt to the child component. The same rule applies to the vertical direction.
  • For a child component of the container, margin has a different meaning from the universal attribute margin. It indicates the distance to the anchor in the respective direction. If there is no anchor in the respective direction, margin in that direction does not take effect.

Child Components

Multiple child components are supported.

APIs

Phone12+PC/2in113+Tablet12+TV19+Wearable18+

RelativeContainer()

The RelativeContainer component is a container component used for relative layout of elements in complex scenarios.

Widget capability: This API can be used in ArkTS widgets since API version 9.

Atomic service API: This API can be used in atomic services since API version 11.

System capability: SystemCapability.ArkUI.ArkUI.Full

Attributes

In addition to the universal attributes, the following attributes are supported.

guideLine12+

Phone12+PC/2in113+Tablet12+TV19+Wearable18+

guideLine(value: Array<GuideLineStyle>)

Sets the guidelines in the RelativeContainer component. The value is an array, each element of which is a guideline.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

Parameters

Expand
Name Type Mandatory Description
value Array<GuideLineStyle> Yes Guidelines in the RelativeContainer component.

barrier12+

Phone12+PC/2in113+Tablet12+TV19+Wearable18+

barrier(value: Array<BarrierStyle>)

Sets the barriers in the RelativeContainer component. The value is an array, each element of which is a barrier.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

Parameters

Expand
Name Type Mandatory Description
value Array<BarrierStyle> Yes Barriers in the RelativeContainer component.

barrier12+

Phone12+PC/2in113+Tablet12+TV19+Wearable18+

barrier(barrierStyle: Array<LocalizedBarrierStyle>)

Sets barriers in the RelativeContainer component. Each array element defines a barrier. Barriers can be defined in mirrored layout mode.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

Parameters

Expand
Name Type Mandatory Description
barrierStyle Array<LocalizedBarrierStyle> Yes Barriers in the RelativeContainer component.

GuideLineStyle12+

Phone12+PC/2in113+Tablet12+TV19+Wearable18+

Defines the ID, direction, and position of a guideline.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

Expand
Name Type Read-Only Optional Description
id string No No ID of the guideline, which must be unique and cannot be the same as the name of any component in the container.
direction Axis No No

Direction of the guideline.

A guideline in the vertical direction can only be used as the anchor of the component in the horizontal direction, and the value is 0 when it is used as the anchor in the vertical direction. A guideline in the horizontal direction can only be used as the anchor of the component in the vertical direction, and the value is 0 when it is used as the anchor in the horizontal direction.

Default value: Axis.Vertical

Invalid values are treated as the default value.

position GuideLinePosition No No

Position of the guideline.

If no value is specified or an invalid value (for example, undefined) is provided, the guideline position defaults to start: 0. Only start or end can be selected for the guideline position. If both are declared, only start takes effect. If the container size in a certain direction is set to "auto", the guideline position in that direction must be declared in start mode, and the value cannot be a percentage.

Default value:

{

start: 0

}

Invalid values are treated as the default value.

GuideLinePosition12+

Phone12+PC/2in113+Tablet12+TV19+Wearable18+

Defines the position of a guideline.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

Expand
Name Type Read-Only Optional Description
start Dimension No Yes Distance between the guideline and the left or top of the container.
end Dimension No Yes Distance between the guideline and the right or bottom of the container.

BarrierStyle12+

Phone12+PC/2in113+Tablet12+TV19+Wearable18+

Defines the ID, direction, and referenced components of a barrier.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

Expand
Name Type Read-Only Optional Description
id string No No ID of the barrier, which must be unique and cannot be the same as the name of any component in the container.
direction BarrierDirection No No

Direction of the barrier.

Vertical-direction barriers (including TOP and BOTTOM) can only serve as the horizontal anchor of a component. If they are used as a vertical anchor, the anchor value will be 0. Horizontal-direction barriers (including LEFT and RIGHT) can only serve as the vertical anchor of a component. If they are used as a horizontal anchor, the anchor value will be 0.

Default value: BarrierDirection.LEFT

Invalid values are treated as the default value.

referencedId Array<string> No No Referenced components of the barrier.

BarrierDirection12+

Phone12+PC/2in113+Tablet12+TV19+Wearable18+

Defines the direction of a barrier.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

Expand
Name Value Description
LEFT 0 The barrier is on the left side of all the referenced components specified by referencedId.
RIGHT 1 The barrier is on the right side of all the referenced components specified by referencedId.
TOP 2 The barrier is at the top of all the referenced components specified by referencedId.
BOTTOM 3 The barrier is at the bottom of all the referenced components specified by referencedId.

LocalizedBarrierStyle12+

Phone12+PC/2in113+Tablet12+TV19+Wearable18+

Defines the ID, direction, and referenced components of a barrier.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

Expand
Name Type Read-Only Optional Description
id string No No ID of the barrier, which must be unique and cannot be the same as the name of any component in the container.
localizedDirection LocalizedBarrierDirection No No

Direction of the barrier.

Vertical-direction barriers (including TOP and BOTTOM) can only serve as the horizontal anchor of a component. If they are used as a vertical anchor, the anchor value will be 0. Horizontal-direction barriers (including START and END) can only serve as the vertical anchor of a component. If they are used as a horizontal anchor, the anchor value will be 0.

referencedId Array<string> No No Referenced components of the barrier.

LocalizedBarrierDirection12+

Phone12+PC/2in113+Tablet12+TV19+Wearable18+

Enumerates the directions of barriers with mirror mode support.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

Expand
Name Value Description
START 0 The barrier is on the left (for left-to-right scripts) or right (for right-to-left scripts) side of all the referenced components specified by referencedId.
END 1 The barrier is on the right (for left-to-right scripts) or left (for right-to-left scripts) side of all the referenced components specified by referencedId.
TOP 2 The barrier is at the top of all the referenced components specified by referencedId.
BOTTOM 3 The barrier is at the bottom of all the referenced components specified by referencedId.

Events

The universal events are supported.

Example

Example 1: Implementing a Layout Using Containers and Components as Anchors

This example demonstrates how to use the alignRules API to implement a layout with containers and their internal components as anchors.

Collapse
Word wrap
Dark theme
Copy code
  1. @Entry
  2. @Component
  3. struct Index {
  4. build() {
  5. Row() {
  6. RelativeContainer() {
  7. Row() {
  8. Text('row1')
  9. }
  10. .justifyContent(FlexAlign.Center)
  11. .width(100)
  12. .height(100)
  13. .backgroundColor('#a3cf62')
  14. .alignRules({
  15. top: { anchor: "__container__", align: VerticalAlign.Top },
  16. left: { anchor: "__container__", align: HorizontalAlign.Start }
  17. })
  18. .id("row1")
  19. Row() {
  20. Text('row2')
  21. }
  22. .justifyContent(FlexAlign.Center)
  23. .width(100)
  24. .height(100)
  25. .backgroundColor('#00ae9d')
  26. .alignRules({
  27. top: { anchor: "__container__", align: VerticalAlign.Top },
  28. right: { anchor: "__container__", align: HorizontalAlign.End }
  29. })
  30. .id("row2")
  31. Row() {
  32. Text('row3')
  33. }
  34. .justifyContent(FlexAlign.Center)
  35. .height(100)
  36. .backgroundColor('#0a59f7')
  37. .alignRules({
  38. top: { anchor: "row1", align: VerticalAlign.Bottom },
  39. left: { anchor: "row1", align: HorizontalAlign.End },
  40. right: { anchor: "row2", align: HorizontalAlign.Start }
  41. })
  42. .id("row3")
  43. Row() {
  44. Text('row4')
  45. }.justifyContent(FlexAlign.Center)
  46. .backgroundColor('#2ca9e0')
  47. .alignRules({
  48. top: { anchor: "row3", align: VerticalAlign.Bottom },
  49. bottom: { anchor: "__container__", align: VerticalAlign.Bottom },
  50. left: { anchor: "__container__", align: HorizontalAlign.Start },
  51. right: { anchor: "row1", align: HorizontalAlign.End }
  52. })
  53. .id("row4")
  54. Row() {
  55. Text('row5')
  56. }.justifyContent(FlexAlign.Center)
  57. .backgroundColor('#30c9f7')
  58. .alignRules({
  59. top: { anchor: "row3", align: VerticalAlign.Bottom },
  60. bottom: { anchor: "__container__", align: VerticalAlign.Bottom },
  61. left: { anchor: "row2", align: HorizontalAlign.Start },
  62. right: { anchor: "__container__", align: HorizontalAlign.End }
  63. })
  64. .id("row5")
  65. }
  66. .width(300).height(300)
  67. .margin({ left: 50 })
  68. .border({ width: 2, color: "#6699FF" })
  69. }
  70. .height('100%')
  71. }
  72. }

Example 2: Setting Margins for Child Components

This example shows how to set margins for child components in the container.

Collapse
Word wrap
Dark theme
Copy code
  1. @Entry
  2. @Component
  3. struct Index {
  4. build() {
  5. Row() {
  6. RelativeContainer() {
  7. Row() {
  8. Text('row1')
  9. }
  10. .justifyContent(FlexAlign.Center)
  11. .width(100)
  12. .height(100)
  13. .backgroundColor('#a3cf62')
  14. .alignRules({
  15. top: { anchor: "__container__", align: VerticalAlign.Top },
  16. left: { anchor: "__container__", align: HorizontalAlign.Start }
  17. })
  18. .id("row1")
  19. .margin(10)
  20. Row() {
  21. Text('row2')
  22. }
  23. .justifyContent(FlexAlign.Center)
  24. .width(100)
  25. .height(100)
  26. .backgroundColor('#00ae9d')
  27. .alignRules({
  28. left: { anchor: "row1", align: HorizontalAlign.End },
  29. top: { anchor: "row1", align: VerticalAlign.Top }
  30. })
  31. .id("row2")
  32. Row() {
  33. Text('row3')
  34. }
  35. .justifyContent(FlexAlign.Center)
  36. .width(100)
  37. .height(100)
  38. .backgroundColor('#0a59f7')
  39. .alignRules({
  40. left: { anchor: "row1", align: HorizontalAlign.Start },
  41. top: { anchor: "row1", align: VerticalAlign.Bottom }
  42. })
  43. .id("row3")
  44. Row() {
  45. Text('row4')
  46. }
  47. .justifyContent(FlexAlign.Center)
  48. .width(100)
  49. .height(100)
  50. .backgroundColor('#2ca9e0')
  51. .alignRules({
  52. left: { anchor: "row3", align: HorizontalAlign.End },
  53. top: { anchor: "row2", align: VerticalAlign.Bottom }
  54. })
  55. .id("row4")
  56. .margin(10)
  57. }
  58. .width(300).height(300)
  59. .margin({ left: 50 })
  60. .border({ width: 2, color: "#6699FF" })
  61. }
  62. .height('100%')
  63. }
  64. }

Example 3: Configuring the Container to Adapt Its Size to Content

This example shows how to configure the container to adapt its size to content by setting width or height to "auto".

Collapse
Word wrap
Dark theme
Copy code
  1. @Entry
  2. @Component
  3. struct Index {
  4. build() {
  5. Row() {
  6. RelativeContainer() {
  7. Row() {
  8. Text('row1')
  9. }
  10. .justifyContent(FlexAlign.Center)
  11. .width(100)
  12. .height(100)
  13. .backgroundColor('#a3cf62')
  14. .id("row1")
  15. Row() {
  16. Text('row2')
  17. }
  18. .justifyContent(FlexAlign.Center)
  19. .width(100)
  20. .height(100)
  21. .backgroundColor('#00ae9d')
  22. .alignRules({
  23. left: { anchor: "row1", align: HorizontalAlign.End },
  24. top: { anchor: "row1", align: VerticalAlign.Top }
  25. })
  26. .id("row2")
  27. Row() {
  28. Text('row3')
  29. }
  30. .justifyContent(FlexAlign.Center)
  31. .width(100)
  32. .height(100)
  33. .backgroundColor('#0a59f7')
  34. .alignRules({
  35. left: { anchor: "row1", align: HorizontalAlign.Start },
  36. top: { anchor: "row1", align: VerticalAlign.Bottom }
  37. })
  38. .id("row3")
  39. Row() {
  40. Text('row4')
  41. }
  42. .justifyContent(FlexAlign.Center)
  43. .width(100)
  44. .height(100)
  45. .backgroundColor('#2ca9e0')
  46. .alignRules({
  47. left: { anchor: "row3", align: HorizontalAlign.End },
  48. top: { anchor: "row2", align: VerticalAlign.Bottom }
  49. })
  50. .id("row4")
  51. }
  52. .width("auto").height("auto")
  53. .margin({ left: 50 })
  54. .border({ width: 2, color: "#6699FF" })
  55. }
  56. .height('100%')
  57. }
  58. }

Example 4: Applying Vertical Offsets

This example uses the bias API to create a vertical offset for a child component between two anchors.

Collapse
Word wrap
Dark theme
Copy code
  1. @Entry
  2. @Component
  3. struct Index {
  4. build() {
  5. Row() {
  6. RelativeContainer() {
  7. Row()
  8. .width(100)
  9. .height(100)
  10. .backgroundColor('#a3cf62')
  11. .alignRules({
  12. top: { anchor: "__container__", align: VerticalAlign.Top },
  13. bottom: { anchor: "__container__", align: VerticalAlign.Bottom },
  14. left: { anchor: "__container__", align: HorizontalAlign.Start },
  15. right: { anchor: "__container__", align: HorizontalAlign.End },
  16. bias: { vertical: 0.3 }
  17. })
  18. .id("row1")
  19. }
  20. .width(300).height(300)
  21. .margin({ left: 50 })
  22. .border({ width: 2, color: "#6699FF" })
  23. }
  24. .height('100%')
  25. }
  26. }

Example 5: Setting Guidelines

This example demonstrates how to set guidelines in a relative layout using the guideLine API, with child components using these guidelines as anchors.

Collapse
Word wrap
Dark theme
Copy code
  1. @Entry
  2. @Component
  3. struct Index {
  4. build() {
  5. Row() {
  6. RelativeContainer() {
  7. Row()
  8. .width(100)
  9. .height(100)
  10. .backgroundColor('#a3cf62')
  11. .alignRules({
  12. left: { anchor: "guideline1", align: HorizontalAlign.End },
  13. top: { anchor: "guideline2", align: VerticalAlign.Top }
  14. })
  15. .id("row1")
  16. }
  17. .width(300)
  18. .height(300)
  19. .margin({ left: 50 })
  20. .border({ width: 2, color: "#6699FF" })
  21. .guideLine([{ id: "guideline1", direction: Axis.Vertical, position: { start: 50 } },
  22. { id: "guideline2", direction: Axis.Horizontal, position: { start: 50 } }])
  23. }
  24. .height('100%')
  25. }
  26. }

Example 6: Implementing Barriers

This example shows how to set barriers in a relative layout using the barrier API, with child components using these barriers as anchors.

Collapse
Word wrap
Dark theme
Copy code
  1. @Entry
  2. @Component
  3. struct Index {
  4. build() {
  5. Row() {
  6. RelativeContainer() {
  7. Row() {
  8. Text('row1')
  9. }
  10. .justifyContent(FlexAlign.Center)
  11. .width(100)
  12. .height(100)
  13. .backgroundColor('#a3cf62')
  14. .id("row1")
  15. Row() {
  16. Text('row2')
  17. }
  18. .justifyContent(FlexAlign.Center)
  19. .width(100)
  20. .height(100)
  21. .backgroundColor('#00ae9d')
  22. .alignRules({
  23. middle: { anchor: "row1", align: HorizontalAlign.End },
  24. top: { anchor: "row1", align: VerticalAlign.Bottom }
  25. })
  26. .id("row2")
  27. Row() {
  28. Text('row3')
  29. }
  30. .justifyContent(FlexAlign.Center)
  31. .width(100)
  32. .height(100)
  33. .backgroundColor('#0a59f7')
  34. .alignRules({
  35. left: { anchor: "barrier1", align: HorizontalAlign.End },
  36. top: { anchor: "row1", align: VerticalAlign.Top }
  37. })
  38. .id("row3")
  39. Row() {
  40. Text('row4')
  41. }
  42. .justifyContent(FlexAlign.Center)
  43. .width(50)
  44. .height(50)
  45. .backgroundColor('#2ca9e0')
  46. .alignRules({
  47. left: { anchor: "row1", align: HorizontalAlign.Start },
  48. top: { anchor: "barrier2", align: VerticalAlign.Bottom }
  49. })
  50. .id("row4")
  51. }
  52. .width(300)
  53. .height(300)
  54. .margin({ left: 50 })
  55. .border({ width: 2, color: "#6699FF" })
  56. .barrier([{ id: "barrier1", direction: BarrierDirection.RIGHT, referencedId: ["row1", "row2"] },
  57. { id: "barrier2", direction: BarrierDirection.BOTTOM, referencedId: ["row1", "row2"] }])
  58. }
  59. .height('100%')
  60. }
  61. }

Example 7: Creating Chains

This example uses the chainMode API to create horizontal chains, including SPREAD, SPREAD_INSIDE, and PACKED chains, from top to bottom.

Collapse
Word wrap
Dark theme
Copy code
  1. @Entry
  2. @Component
  3. struct Index {
  4. build() {
  5. Row() {
  6. RelativeContainer() {
  7. Row() {
  8. Text('row1')
  9. }
  10. .justifyContent(FlexAlign.Center)
  11. .width(80)
  12. .height(80)
  13. .backgroundColor('#a3cf62')
  14. .alignRules({
  15. left: { anchor: "__container__", align: HorizontalAlign.Start },
  16. right: { anchor: "row2", align: HorizontalAlign.Start },
  17. top: { anchor: "__container__", align: VerticalAlign.Top }
  18. })
  19. .id("row1")
  20. .chainMode(Axis.Horizontal, ChainStyle.SPREAD)
  21. Row() {
  22. Text('row2')
  23. }
  24. .justifyContent(FlexAlign.Center)
  25. .width(80)
  26. .height(80)
  27. .backgroundColor('#00ae9d')
  28. .alignRules({
  29. left: { anchor: "row1", align: HorizontalAlign.End },
  30. right: { anchor: "row3", align: HorizontalAlign.Start },
  31. top: { anchor: "row1", align: VerticalAlign.Top }
  32. })
  33. .id("row2")
  34. Row() {
  35. Text('row3')
  36. }
  37. .justifyContent(FlexAlign.Center)
  38. .width(80)
  39. .height(80)
  40. .backgroundColor('#0a59f7')
  41. .alignRules({
  42. left: { anchor: "row2", align: HorizontalAlign.End },
  43. right: { anchor: "__container__", align: HorizontalAlign.End },
  44. top: { anchor: "row1", align: VerticalAlign.Top }
  45. })
  46. .id("row3")
  47. Row() {
  48. Text('row4')
  49. }
  50. .justifyContent(FlexAlign.Center)
  51. .width(80)
  52. .height(80)
  53. .backgroundColor('#a3cf62')
  54. .alignRules({
  55. left: { anchor: "__container__", align: HorizontalAlign.Start },
  56. right: { anchor: "row5", align: HorizontalAlign.Start },
  57. center: { anchor: "__container__", align: VerticalAlign.Center }
  58. })
  59. .id("row4")
  60. .chainMode(Axis.Horizontal, ChainStyle.SPREAD_INSIDE)
  61. Row() {
  62. Text('row5')
  63. }
  64. .justifyContent(FlexAlign.Center)
  65. .width(80)
  66. .height(80)
  67. .backgroundColor('#00ae9d')
  68. .alignRules({
  69. left: { anchor: "row4", align: HorizontalAlign.End },
  70. right: { anchor: "row6", align: HorizontalAlign.Start },
  71. top: { anchor: "row4", align: VerticalAlign.Top }
  72. })
  73. .id("row5")
  74. Row() {
  75. Text('row6')
  76. }
  77. .justifyContent(FlexAlign.Center)
  78. .width(80)
  79. .height(80)
  80. .backgroundColor('#0a59f7')
  81. .alignRules({
  82. left: { anchor: "row5", align: HorizontalAlign.End },
  83. right: { anchor: "__container__", align: HorizontalAlign.End },
  84. top: { anchor: "row4", align: VerticalAlign.Top }
  85. })
  86. .id("row6")
  87. Row() {
  88. Text('row7')
  89. }
  90. .justifyContent(FlexAlign.Center)
  91. .width(80)
  92. .height(80)
  93. .backgroundColor('#a3cf62')
  94. .alignRules({
  95. left: { anchor: "__container__", align: HorizontalAlign.Start },
  96. right: { anchor: "row8", align: HorizontalAlign.Start },
  97. bottom: { anchor: "__container__", align: VerticalAlign.Bottom }
  98. })
  99. .id("row7")
  100. .chainMode(Axis.Horizontal, ChainStyle.PACKED)
  101. Row() {
  102. Text('row8')
  103. }
  104. .justifyContent(FlexAlign.Center)
  105. .width(80)
  106. .height(80)
  107. .backgroundColor('#00ae9d')
  108. .alignRules({
  109. left: { anchor: "row7", align: HorizontalAlign.End },
  110. right: { anchor: "row9", align: HorizontalAlign.Start },
  111. top: { anchor: "row7", align: VerticalAlign.Top }
  112. })
  113. .id("row8")
  114. Row() {
  115. Text('row9')
  116. }
  117. .justifyContent(FlexAlign.Center)
  118. .width(80)
  119. .height(80)
  120. .backgroundColor('#0a59f7')
  121. .alignRules({
  122. left: { anchor: "row8", align: HorizontalAlign.End },
  123. right: { anchor: "__container__", align: HorizontalAlign.End },
  124. top: { anchor: "row7", align: VerticalAlign.Top }
  125. })
  126. .id("row9")
  127. }
  128. .width(300).height(300)
  129. .margin({ left: 50 })
  130. .border({ width: 2, color: "#6699FF" })
  131. }
  132. .height('100%')
  133. }
  134. }

Example 8: Creating a Chain with Offsets

This example combines the chainMode and bias APIs to create a horizontal PACKED chain with offsets.

Collapse
Word wrap
Dark theme
Copy code
  1. @Entry
  2. @Component
  3. struct Index {
  4. build() {
  5. Row() {
  6. RelativeContainer() {
  7. Row() {
  8. Text('row1')
  9. }
  10. .justifyContent(FlexAlign.Center)
  11. .width(80)
  12. .height(80)
  13. .backgroundColor('#a3cf62')
  14. .alignRules({
  15. left: { anchor: "__container__", align: HorizontalAlign.Start },
  16. right: { anchor: "row2", align: HorizontalAlign.Start },
  17. center: { anchor: "__container__", align: VerticalAlign.Center },
  18. bias: { horizontal: 0 }
  19. })
  20. .id("row1")
  21. .chainMode(Axis.Horizontal, ChainStyle.PACKED)
  22. Row() {
  23. Text('row2')
  24. }
  25. .justifyContent(FlexAlign.Center)
  26. .width(80)
  27. .height(80)
  28. .backgroundColor('#00ae9d')
  29. .alignRules({
  30. left: { anchor: "row1", align: HorizontalAlign.End },
  31. right: { anchor: "row3", align: HorizontalAlign.Start },
  32. top: { anchor: "row1", align: VerticalAlign.Top }
  33. })
  34. .id("row2")
  35. Row() {
  36. Text('row3')
  37. }
  38. .justifyContent(FlexAlign.Center)
  39. .width(80)
  40. .height(80)
  41. .backgroundColor('#0a59f7')
  42. .alignRules({
  43. left: { anchor: "row2", align: HorizontalAlign.End },
  44. right: { anchor: "__container__", align: HorizontalAlign.End },
  45. top: { anchor: "row1", align: VerticalAlign.Top }
  46. })
  47. .id("row3")
  48. }
  49. .width(300).height(300)
  50. .margin({ left: 50 })
  51. .border({ width: 2, color: "#6699FF" })
  52. }
  53. .height('100%')
  54. }
  55. }

Example 9: Implementing a Mirror Effect

This example demonstrates how to use LocalizedAlignRuleOptions and LocalizedBarrierDirection for alignment when using barriers as anchors in mirror mode (direction set to Direction.Rtl).

Collapse
Word wrap
Dark theme
Copy code
  1. @Entry
  2. @Component
  3. struct Index {
  4. build() {
  5. Row() {
  6. RelativeContainer() {
  7. Row() {
  8. Text('row1')
  9. }
  10. .justifyContent(FlexAlign.Center)
  11. .width(100)
  12. .height(100)
  13. .backgroundColor('#a3cf62')
  14. .id("row1")
  15. Row() {
  16. Text('row2')
  17. }
  18. .justifyContent(FlexAlign.Center)
  19. .width(100)
  20. .height(100)
  21. .backgroundColor('#00ae9d')
  22. .alignRules({
  23. middle: { anchor: "row1", align: HorizontalAlign.End },
  24. top: { anchor: "row1", align: VerticalAlign.Bottom }
  25. })
  26. .id("row2")
  27. Row() {
  28. Text('row3')
  29. }
  30. .justifyContent(FlexAlign.Center)
  31. .width(100)
  32. .height(100)
  33. .backgroundColor('#0a59f7')
  34. .alignRules({
  35. start: { anchor: "barrier1", align: HorizontalAlign.End },
  36. top: { anchor: "row1", align: VerticalAlign.Top }
  37. })
  38. .id("row3")
  39. Row() {
  40. Text('row4')
  41. }
  42. .justifyContent(FlexAlign.Center)
  43. .width(50)
  44. .height(50)
  45. .backgroundColor('#2ca9e0')
  46. .alignRules({
  47. start: { anchor: "row1", align: HorizontalAlign.Start },
  48. top: { anchor: "barrier2", align: VerticalAlign.Bottom }
  49. })
  50. .id("row4")
  51. }
  52. .direction(Direction.Rtl)
  53. .width(300)
  54. .height(300)
  55. .margin({ left: 50 })
  56. .border({ width: 2, color: "#6699FF" })
  57. .barrier([{ id: "barrier1", localizedDirection: LocalizedBarrierDirection.END, referencedId: ["row1", "row2"] },
  58. { id: "barrier2", localizedDirection: LocalizedBarrierDirection.BOTTOM, referencedId: ["row1", "row2"] }])
  59. }
  60. .height('100%')
  61. }
  62. }

Example 10: Setting Component Weights in a Chain

This example demonstrates how to use chainWeight to set the size weights of components in a chain.

Collapse
Word wrap
Dark theme
Copy code
  1. @Entry
  2. @Component
  3. struct Index {
  4. build() {
  5. Row() {
  6. RelativeContainer() {
  7. Row() {
  8. Text('row1')
  9. }
  10. .justifyContent(FlexAlign.Center)
  11. .width(80)
  12. .height(80)
  13. .backgroundColor('#a3cf62')
  14. .alignRules({
  15. left: { anchor: "__container__", align: HorizontalAlign.Start },
  16. right: { anchor: "row2", align: HorizontalAlign.Start },
  17. center: { anchor: "__container__", align: VerticalAlign.Center },
  18. })
  19. .id("row1")
  20. .chainMode(Axis.Horizontal, ChainStyle.PACKED)
  21. Row() {
  22. Text('row2')
  23. }
  24. .justifyContent(FlexAlign.Center)
  25. .width(80)
  26. .height(80)
  27. .backgroundColor('#00ae9d')
  28. .alignRules({
  29. left: { anchor: "row1", align: HorizontalAlign.End },
  30. right: { anchor: "row3", align: HorizontalAlign.Start },
  31. top: { anchor: "row1", align: VerticalAlign.Top }
  32. })
  33. .id("row2")
  34. .chainWeight({ horizontal: 1 })
  35. Row() {
  36. Text('row3')
  37. }
  38. .justifyContent(FlexAlign.Center)
  39. .width(80)
  40. .height(80)
  41. .backgroundColor('#0a59f7')
  42. .alignRules({
  43. left: { anchor: "row2", align: HorizontalAlign.End },
  44. right: { anchor: "__container__", align: HorizontalAlign.End },
  45. top: { anchor: "row1", align: VerticalAlign.Top }
  46. })
  47. .id("row3")
  48. .chainWeight({ horizontal: 2 })
  49. }
  50. .width(300).height(300)
  51. .margin({ left: 50 })
  52. .border({ width: 2, color: "#6699FF" })
  53. }
  54. .height('100%')
  55. }
  56. }

Search in References
Enter a keyword.