相對(duì)布局(RelativeContainer)

2024-01-25 13:11 更新

概述

RelativeContainer為采用相對(duì)布局的容器,支持容器內(nèi)部的子元素設(shè)置相對(duì)位置關(guān)系。子元素支持指定兄弟元素作為錨點(diǎn),也支持指定父容器作為錨點(diǎn),基于錨點(diǎn)做相對(duì)位置布局。下圖是一個(gè)RelativeContainer的概念圖,圖中的虛線表示位置的依賴關(guān)系。

圖1 相對(duì)布局示意圖

子元素并不完全是上圖中的依賴關(guān)系。比如,Item4可以以Item2為依賴錨點(diǎn),也可以以RelativeContainer父容器為依賴錨點(diǎn)。

基本概念

  • 錨點(diǎn):通過(guò)錨點(diǎn)設(shè)置當(dāng)前元素基于哪個(gè)元素確定位置。
  • 對(duì)齊方式:通過(guò)對(duì)齊方式,設(shè)置當(dāng)前元素是基于錨點(diǎn)的上中下對(duì)齊,還是基于錨點(diǎn)的左中右對(duì)齊。

設(shè)置依賴關(guān)系

錨點(diǎn)設(shè)置

錨點(diǎn)設(shè)置是指設(shè)置子元素相對(duì)于父元素或兄弟元素的位置依賴關(guān)系。在水平方向上,可以設(shè)置left、middle、right的錨點(diǎn)。在豎直方向上,可以設(shè)置top、center、bottom的錨點(diǎn)。為了明確定義錨點(diǎn),必須為RelativeContainer及其子元素設(shè)置ID,用于指定錨點(diǎn)信息。ID默認(rèn)為“__container__”,其余子元素的ID通過(guò)id屬性設(shè)置。未設(shè)置ID的子元素在RelativeContainer中不會(huì)顯示。

說(shuō)明

在使用錨點(diǎn)時(shí)要注意子元素的相對(duì)位置關(guān)系,避免出現(xiàn)錯(cuò)位或遮擋的情況。

  • RelativeContainer父組件為錨點(diǎn),__container__代表父容器的id。
    1. RelativeContainer() {
    2. Row()
    3. // 添加其他屬性
    4. .alignRules({
    5. top: { anchor: '__container__', align: VerticalAlign.Top },
    6. left: { anchor: '__container__', align: HorizontalAlign.Start }
    7. })
    8. .id("row1")
    9. Row()
    10. ...
    11. .alignRules({
    12. top: { anchor: '__container__', align: VerticalAlign.Top },
    13. right: { anchor: '__container__', align: HorizontalAlign.End }
    14. })
    15. .id("row2")
    16. }
    17. ...

  • 以子元素為錨點(diǎn)。
    1. RelativeContainer() {
    2. ...
    3. top: { anchor: 'row1', align: VerticalAlign.Bottom },
    4. ...
    5. }
    6. .width(300).height(300)
    7. .margin({ left: 20 })
    8. .border({ width: 2, color: '#6699FF' })

設(shè)置相對(duì)于錨點(diǎn)的對(duì)齊位置

設(shè)置了錨點(diǎn)之后,可以通過(guò)align設(shè)置相對(duì)于錨點(diǎn)的對(duì)齊位置。

在水平方向上,對(duì)齊位置可以設(shè)置為HorizontalAlign.Start、HorizontalAlign.Center、HorizontalAlign.End。

在豎直方向上,對(duì)齊位置可以設(shè)置為VerticalAlign.Top、VerticalAlign.Center、VerticalAlign.Bottom。

場(chǎng)景實(shí)例

相對(duì)布局內(nèi)的子元素相對(duì)靈活,只要在RelativeContainer容器內(nèi),均可以通過(guò)alignRules進(jìn)行相應(yīng)的位置移動(dòng)。

  1. @Entry
  2. @Component
  3. struct Index {
  4. build() {
  5. Row() {
  6. RelativeContainer() {
  7. Row()
  8. .width(100)
  9. .height(100)
  10. .backgroundColor('#FF3333')
  11. .alignRules({
  12. top: { anchor: '__container__', align: VerticalAlign.Top }, //以父容器為錨點(diǎn),豎直方向頂頭對(duì)齊
  13. middle: { anchor: '__container__', align: HorizontalAlign.Center } //以父容器為錨點(diǎn),水平方向居中對(duì)齊
  14. })
  15. .id('row1') //設(shè)置錨點(diǎn)為row1
  16. Row() {
  17. Image($r('app.media.icon'))
  18. }
  19. .height(100).width(100)
  20. .alignRules({
  21. top: { anchor: 'row1', align: VerticalAlign.Bottom }, //以row1組件為錨點(diǎn),豎直方向底端對(duì)齊
  22. left: { anchor: 'row1', align: HorizontalAlign.Start } //以row1組件為錨點(diǎn),水平方向開(kāi)頭對(duì)齊
  23. })
  24. .id('row2') //設(shè)置錨點(diǎn)為row2
  25. Row()
  26. .width(100)
  27. .height(100)
  28. .backgroundColor('#FFCC00')
  29. .alignRules({
  30. top: { anchor: 'row2', align: VerticalAlign.Top }
  31. })
  32. .id('row3') //設(shè)置錨點(diǎn)為row3
  33. Row()
  34. .width(100)
  35. .height(100)
  36. .backgroundColor('#FF9966')
  37. .alignRules({
  38. top: { anchor: 'row2', align: VerticalAlign.Top },
  39. left: { anchor: 'row2', align: HorizontalAlign.End },
  40. })
  41. .id('row4') //設(shè)置錨點(diǎn)為row4
  42. Row()
  43. .width(100)
  44. .height(100)
  45. .backgroundColor('#FF66FF')
  46. .alignRules({
  47. top: { anchor: 'row2', align: VerticalAlign.Bottom },
  48. middle: { anchor: 'row2', align: HorizontalAlign.Center }
  49. })
  50. .id('row5') //設(shè)置錨點(diǎn)為row5
  51. }
  52. .width(300).height(300)
  53. .border({ width: 2, color: '#6699FF' })
  54. }
  55. .height('100%').margin({ left: 30 })
  56. }
  57. }

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)