概述

在应用的开发过程中,经常需要设计复杂界面,此时涉及到多个相同或不同组件之间的嵌套。如果布局组件嵌套深度过深,或者嵌套组件数过多,会带来额外的开销。如果在布局的方式上进行优化,就可以有效的提升性能,减少时间开销。

RelativeContainer是一种采用相对布局的容器,支持容器内部的子元素设置相对位置关系,适用于处理界面复杂的场景,对多个子元素进行对齐和排列。子元素可以指定兄弟元素或父容器作为锚点,基于锚点进行相对位置布局。在使用锚点时,需注意子元素的相对位置关系,以避免出现错位或遮挡的情况。下图展示了一个 RelativeContainer的概念图,图中的虚线表示位置的依赖关系。

图1 相对布局示意图

子元素并不完全是上图中的依赖关系。比如,Item4可以以Item2为依赖锚点,也可以以RelativeContainer父容器为依赖锚点。

基本概念

  • 参考边界:设置当前组件的哪个边界对齐到锚点。

  • 锚点:通过锚点设置当前元素基于哪个元素确定位置。

  • 对齐方式:通过对齐方式,设置当前元素是基于锚点的上中下对齐,还是基于锚点的左中右对齐。

  • 链:将一系列组件以首尾相连的方式对齐,可以形成一条链。通过设置链的模式,可以指定链上元素的排列方式。

  • 辅助线:辅助线是在容器内虚拟出的额外水平或垂直锚点,便于统一对齐至某个偏移位置。

  • 屏障:屏障是指容器内一组指定组件在特定方向上的共同最远边界,例如,一组组件下方的屏障,是指这些组件底部边缘中最底部的那个边界。

设置依赖关系

设置参考边界

设置当前组件的哪个边界对齐到锚点。容器内子组件的参考边界区分水平方向和垂直方向。

  • 在水平方向上,可以按照起始(left)、居中(middle)或尾端(right)的组件边界与锚点对齐。当设置三个边界时,仅起始(left)和居中(middle)的边界设置生效。

  • 在垂直方向上,可以设置组件边界与锚点对齐,具体包括顶部(top)、居中(center)和底部(bottom)。当设置三个边界时,仅顶部(top)和居中(center)生效。

设置锚点

锚点设置涉及子元素相对于其父元素或兄弟元素的位置依赖关系。具体而言,子元素可以将其位置锚定到相对布局容器(RelativeContainer)、辅助线(guideline)、屏障(barrier)或其他子元素上。

为了准确定义锚点,RelativeContainer的子元素必须拥有唯一的组件标识(id),用于指定锚点信息。父元素RelativeContainer的标识默认为“__container__”,其他子元素的组件标识(id)则通过id属性设置。

说明

  • 未设置组件标识(id)的组件虽可显示,但无法被其他组件引用为锚点。相对布局容器会为其拼接组件标识,但组件标识(id)的规律无法被应用感知。辅助线(guideline)与屏障(barrier)的组件标识(id)需确保唯一,避免与任何组件冲突。若有重复,遵循组件 > guideline > barrier 的优先级。
  • 组件间设置锚点时应避免形成依赖循环(组件之间设置链除外),依赖循环将导致子组件缺乏定位基准,最终无法绘制。
  • RelativeContainer父组件为锚点,__container__代表父容器的组件标识(id)。

    
      
    1. let alignRus: Record<string, Record<string, string | VerticalAlign | HorizontalAlign>> = {
    2. 'top': { 'anchor': '__container__', 'align': VerticalAlign.Top },
    3. 'left': { 'anchor': '__container__', 'align': HorizontalAlign.Start }
    4. }
    5. let alignRue: Record<string, Record<string, string | VerticalAlign | HorizontalAlign>> = {
    6. 'top': { 'anchor': '__container__', 'align': VerticalAlign.Top },
    7. 'right': { 'anchor': '__container__', 'align': HorizontalAlign.End }
    8. }
    9. let marginLeft: Record<string, number> = { 'left': 20 }
    10. let bwc: Record<string, number | string> = { 'width': 2, 'color': '#6699FF' }
    11. @Entry
    12. @Component
    13. struct ParentRefRelativeContainer {
    14. build() {
    15. RelativeContainer() {
    16. Row() {
    17. Text('row1')
    18. }
    19. .justifyContent(FlexAlign.Center)
    20. .width(100)
    21. .height(100)
    22. .backgroundColor('#a3cf62')
    23. .alignRules(alignRus)
    24. .id('row1')
    25. Row() {
    26. Text('row2')
    27. }
    28. .justifyContent(FlexAlign.Center)
    29. .width(100)
    30. .height(100)
    31. .backgroundColor('#00ae9d')
    32. .alignRules(alignRue)
    33. .id('row2')
    34. }.width(300).height(300)
    35. .margin(marginLeft)
    36. .border(bwc)
    37. }
    38. }

    RelativeContainerParentComponentId.ets

  • 以兄弟元素为锚点。

    
      
    1. let alignRus001: Record<string, Record<string, string | VerticalAlign | HorizontalAlign>> = {
    2. 'top': { 'anchor': '__container__', 'align': VerticalAlign.Top },
    3. 'left': { 'anchor': '__container__', 'align': HorizontalAlign.Start }
    4. }
    5. let relConB: Record<string, Record<string, string | VerticalAlign | HorizontalAlign>> = {
    6. 'top': { 'anchor': 'row1', 'align': VerticalAlign.Bottom },
    7. 'left': { 'anchor': 'row1', 'align': HorizontalAlign.Start }
    8. }
    9. let marginLeft001: Record<string, number> = { 'left': 20 }
    10. let bwc001: Record<string, number | string> = { 'width': 2, 'color': '#6699FF' }
    11. @Entry
    12. @Component
    13. struct SiblingRefRelativeContainer {
    14. build() {
    15. RelativeContainer() {
    16. Row() {
    17. Text('row1')
    18. }
    19. .justifyContent(FlexAlign.Center)
    20. .width(100)
    21. .height(100)
    22. .backgroundColor('#00ae9d')
    23. .alignRules(alignRus001)
    24. .id('row1')
    25. Row() {
    26. Text('row2')
    27. }
    28. .justifyContent(FlexAlign.Center)
    29. .width(100)
    30. .height(100)
    31. .backgroundColor('#a3cf62')
    32. .alignRules(relConB)
    33. .id('row2')
    34. }.width(300).height(300)
    35. .margin(marginLeft001)
    36. .border(bwc001)
    37. }
    38. }

    RelativeContainerSiblingComponentId.ets

  • 子组件锚点可以任意选择,但需注意不要相互依赖。

    
      
    1. @Entry
    2. @Component
    3. struct ChildRefRelativeContainer {
    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. .backgroundColor('#00ae9d')
    25. .alignRules({
    26. top: { anchor: '__container__', align: VerticalAlign.Top },
    27. right: { anchor: '__container__', align: HorizontalAlign.End },
    28. bottom: { anchor: 'row1', align: VerticalAlign.Center },
    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.Start },
    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. left: { anchor: 'row1', align: HorizontalAlign.Center },
    50. right: { anchor: 'row2', align: HorizontalAlign.End },
    51. bottom: { anchor: '__container__', align: VerticalAlign.Bottom }
    52. })
    53. .id('row4')
    54. }
    55. .width(300).height(300)
    56. .margin({ left: 50 })
    57. .border({ width: 2, color: '#6699FF' })
    58. }
    59. .height('100%')
    60. }
    61. }

    RelativeContainerChildComponentId.ets

设置相对于锚点的对齐位置

设置了锚点之后,可以通过alignRules属性的align设置相对于锚点的对齐位置。

在水平方向上,对齐位置可以设置为HorizontalAlign.Start、HorizontalAlign.Center、HorizontalAlign.End。

在垂直方向上,对齐位置可以设置为VerticalAlign.Top、VerticalAlign.Center、VerticalAlign.Bottom。

子组件位置偏移

子组件经过相对位置对齐后,可能尚未达到目标位置。开发者可根据需要设置额外偏移(offset)。当使用offset调整位置的组件作为锚点时,对齐位置为设置offset之前的位置。从API Version 11开始,新增了Bias对象,建议API Version 11及以后的版本使用bias来设置额外偏移。使用bias的示例可以参考示例4(设置偏移)


  1. @Entry
  2. @Component
  3. struct ChildComponentOffsetExample {
  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. .backgroundColor('#00ae9d')
  25. .alignRules({
  26. top: { anchor: '__container__', align: VerticalAlign.Top },
  27. right: { anchor: '__container__', align: HorizontalAlign.End },
  28. bottom: { anchor: 'row1', align: VerticalAlign.Center },
  29. })
  30. .offset({
  31. x: -40,
  32. y: -20
  33. })
  34. .id('row2')
  35. Row() {
  36. Text('row3')
  37. }
  38. .justifyContent(FlexAlign.Center)
  39. .height(100)
  40. .backgroundColor('#0a59f7')
  41. .alignRules({
  42. top: { anchor: 'row1', align: VerticalAlign.Bottom },
  43. left: { anchor: 'row1', align: HorizontalAlign.End },
  44. right: { anchor: 'row2', align: HorizontalAlign.Start }
  45. })
  46. .offset({
  47. x: -10,
  48. y: -20
  49. })
  50. .id('row3')
  51. Row() {
  52. Text('row4')
  53. }
  54. .justifyContent(FlexAlign.Center)
  55. .backgroundColor('#2ca9e0')
  56. .alignRules({
  57. top: { anchor: 'row3', align: VerticalAlign.Bottom },
  58. bottom: { anchor: '__container__', align: VerticalAlign.Bottom },
  59. left: { anchor: '__container__', align: HorizontalAlign.Start },
  60. right: { anchor: 'row1', align: HorizontalAlign.End }
  61. })
  62. .offset({
  63. x: -10,
  64. y: -30
  65. })
  66. .id('row4')
  67. Row() {
  68. Text('row5')
  69. }
  70. .justifyContent(FlexAlign.Center)
  71. .backgroundColor('#30c9f7')
  72. .alignRules({
  73. top: { anchor: 'row3', align: VerticalAlign.Bottom },
  74. bottom: { anchor: '__container__', align: VerticalAlign.Bottom },
  75. left: { anchor: 'row2', align: HorizontalAlign.Start },
  76. right: { anchor: 'row2', align: HorizontalAlign.End }
  77. })
  78. .offset({
  79. x: 10,
  80. y: 20
  81. })
  82. .id('row5')
  83. Row() {
  84. Text('row6')
  85. }
  86. .justifyContent(FlexAlign.Center)
  87. .backgroundColor('#ff33ffb5')
  88. .alignRules({
  89. top: { anchor: 'row3', align: VerticalAlign.Bottom },
  90. bottom: { anchor: 'row4', align: VerticalAlign.Bottom },
  91. left: { anchor: 'row3', align: HorizontalAlign.Start },
  92. right: { anchor: 'row3', align: HorizontalAlign.End }
  93. })
  94. .offset({
  95. x: -15,
  96. y: 10
  97. })
  98. .backgroundImagePosition(Alignment.Bottom)
  99. .backgroundImageSize(ImageSize.Cover)
  100. .id('row6')
  101. }
  102. .width(300).height(300)
  103. .margin({ left: 50 })
  104. .border({ width: 2, color: '#6699FF' })
  105. }
  106. .height('100%')
  107. }
  108. }

RelativeContainerChildComponentOffset.ets

多种组件的对齐布局

RowColumnFlexStack等多种布局组件,可按照RelativeContainer组件规则进行对齐排布。


  1. @Entry
  2. @Component
  3. struct RelativeContainerExample {
  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. left: { anchor: '__container__', align: HorizontalAlign.Start }
  14. })
  15. .id('row1')
  16. Column()
  17. .width('50%')
  18. .height(30)
  19. .backgroundColor('#00ae9d')
  20. .alignRules({
  21. top: { anchor: '__container__', align: VerticalAlign.Top },
  22. left: { anchor: '__container__', align: HorizontalAlign.Center }
  23. })
  24. .id('row2')
  25. Flex({ direction: FlexDirection.Row }) {
  26. Text('1').width('20%').height(50).backgroundColor('#0a59f7')
  27. Text('2').width('20%').height(50).backgroundColor('#2ca9e0')
  28. Text('3').width('20%').height(50).backgroundColor('#0a59f7')
  29. Text('4').width('20%').height(50).backgroundColor('#2ca9e0')
  30. }
  31. .padding(10)
  32. .backgroundColor('#30c9f7')
  33. .alignRules({
  34. top: { anchor: 'row2', align: VerticalAlign.Bottom },
  35. left: { anchor: '__container__', align: HorizontalAlign.Start },
  36. bottom: { anchor: '__container__', align: VerticalAlign.Center },
  37. right: { anchor: 'row2', align: HorizontalAlign.Center }
  38. })
  39. .id('row3')
  40. Stack({ alignContent: Alignment.Bottom }) {
  41. Text('First child, show in bottom')
  42. .width('90%')
  43. .height('100%')
  44. .backgroundColor('#a3cf62')
  45. .align(Alignment.Top)
  46. Text('Second child, show in top').width('70%').height('60%').backgroundColor('#00ae9d').align(Alignment.Top)
  47. }
  48. .margin({ top: 5 })
  49. .alignRules({
  50. top: { anchor: 'row3', align: VerticalAlign.Bottom },
  51. left: { anchor: '__container__', align: HorizontalAlign.Start },
  52. bottom: { anchor: '__container__', align: VerticalAlign.Bottom },
  53. right: { anchor: 'row3', align: HorizontalAlign.End }
  54. })
  55. .id('row4')
  56. }
  57. .width(300).height(300)
  58. .margin({ left: 50 })
  59. .border({ width: 2, color: '#6699FF' })
  60. }
  61. .height('100%')
  62. }
  63. }

RelativeContainerDifferentComponentId.ets

以上内容均参考于相对布局 (RelativeContainer)-构建布局-组件布局-UI开发 (ArkTS声明式开发范式)-ArkUI(方舟UI框架)-应用框架 - 华为HarmonyOS开发者

实战演示

Logo

讨论HarmonyOS开发技术,专注于API与组件、DevEco Studio、测试、元服务和应用上架分发等。

更多推荐