当前位置:   article > 正文

相对布局(RelativeContainer)

relativecontainer

目录

1、概述

2、基本概念

3、设置依赖关系

3.1、锚点设置

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

3.2.1 HorizontalAlign.Start

3.2.2 HorizontalAlign.Center

3.2.3 HorizontalAlign.End

3.2.4 VerticalAlign.Top

3.2.5 VerticalAlign.Center

3.2.6 VerticalAlign.Bottom

4、场景实例


1、概述

        RelativeContainer为采用相对布局的容器,支持容器内部的子元素设置相对位置关系。子元素支持指定兄弟元素作为锚点,也支持指定父容器作为锚点,基于锚点做相对位置布局。下图是一个RelativeContainer的概念图,图中的虚线表示位置的依赖关系。

图1 相对布局示意图

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

2、基本概念

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

3、设置依赖关系

3.1、锚点设置

        锚点设置是指设置子元素相对于父元素或兄弟元素的位置依赖关系。在水平方向上,可以设置left、middle、right的锚点。在竖直方向上,可以设置top、center、bottom的锚点。为了明确定义锚点,必须为RelativeContainer及其子元素设置ID,用于指定锚点信息。ID默认为“__container__”,其余子元素的ID通过id属性设置。未设置ID的子元素在RelativeContainer中不会显示。

说明

在使用锚点时要注意子元素的相对位置关系,避免出现错位或遮挡的情况。

  • RelativeContainer父组件为锚点,__container__代表父容器的id。
  1. @Entry
  2. @Component
  3. struct RelativeContainerPage {
  4. @State message: string = 'Hello World'
  5. build() {
  6. Row() {
  7. RelativeContainerView()
  8. }
  9. .height('100%')
  10. }
  11. }
  12. @Component
  13. struct RelativeContainerView {
  14. build() {
  15. RelativeContainer() {
  16. Row()
  17. .alignRules({
  18. top: { anchor: '__container__', align: VerticalAlign.Top },
  19. left: { anchor: '__container__', align: HorizontalAlign.Start }
  20. }).width('20%').height(100)
  21. .id("row1").backgroundColor("#ff0000")
  22. Row()
  23. .alignRules({
  24. top: { anchor: '__container__', align: VerticalAlign.Top },
  25. right: {anchor: '__container__', align: HorizontalAlign.End}
  26. }).width('20%').height(100)
  27. .id("row2").backgroundColor("#ffcc00")
  28. }.width('100%').height('100%')
  29. }
  30. }

  • 以子元素为锚点。
  1. @Entry
  2. @Component
  3. struct RelativeContainerPage {
  4. @State message: string = 'Hello World'
  5. build() {
  6. Row() {
  7. RelativeContainerView()
  8. }
  9. .height('100%')
  10. }
  11. }
  12. @Component
  13. struct RelativeContainerView {
  14. build() {
  15. RelativeContainer() {
  16. Row()
  17. .alignRules({
  18. top: { anchor: '__container__', align: VerticalAlign.Top },
  19. left: { anchor: '__container__', align: HorizontalAlign.Start }
  20. }).width('20%').height(100)
  21. .id("row1").backgroundColor("#ff0000")
  22. Row()
  23. .alignRules({
  24. top: { anchor: 'row1', align: VerticalAlign.Top },
  25. left: {anchor: 'row1', align: HorizontalAlign.End}
  26. }).width('20%').height(100)
  27. .margin({
  28. top: 12,
  29. left: 12
  30. })
  31. .id("row2").backgroundColor("#ffcc00")
  32. }.width('100%').height('100%')
  33. }
  34. }

 

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

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

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

3.2.1 HorizontalAlign.Start
  1. @Entry
  2. @Component
  3. struct RelativeContainerPage {
  4. @State message: string = 'Hello World'
  5. build() {
  6. Row() {
  7. RelativeContainerView()
  8. }
  9. .height('100%')
  10. }
  11. }
  12. @Component
  13. struct RelativeContainerView {
  14. build() {
  15. RelativeContainer() {
  16. Text('Anchor')
  17. .backgroundColor('#aa67c8ff')
  18. .id('anchor')
  19. .width('40%')
  20. .textAlign(TextAlign.Center)
  21. .alignRules({
  22. center: { anchor: '__container__', align: VerticalAlign.Center},
  23. middle: {anchor: '__container__', align: HorizontalAlign.Center}
  24. })
  25. Column()
  26. .alignRules({
  27. left: {anchor: 'anchor', align: HorizontalAlign.Start}
  28. })
  29. .width(12)
  30. .height('100%')
  31. .backgroundColor('#80ff0000').id('vLine')
  32. }
  33. .width('100%')
  34. .height('20%')
  35. .backgroundColor('#999')
  36. .alignSelf(ItemAlign.Start)
  37. }
  38. }

 

3.2.2 HorizontalAlign.Center
  1. @Entry
  2. @Component
  3. struct RelativeContainerPage {
  4. @State message: string = 'Hello World'
  5. build() {
  6. Row() {
  7. RelativeContainerView()
  8. }
  9. .height('100%')
  10. }
  11. }
  12. @Component
  13. struct RelativeContainerView {
  14. build() {
  15. RelativeContainer() {
  16. Text('Anchor')
  17. .backgroundColor('#aa67c8ff')
  18. .id('anchor')
  19. .width('40%')
  20. .textAlign(TextAlign.Center)
  21. .alignRules({
  22. center: { anchor: '__container__', align: VerticalAlign.Center},
  23. middle: {anchor: '__container__', align: HorizontalAlign.Center}
  24. })
  25. Column()
  26. .alignRules({
  27. middle: {anchor: 'anchor', align: HorizontalAlign.Center}
  28. })
  29. .width(12)
  30. .height('100%')
  31. .backgroundColor('#80ff0000').id('vLine')
  32. }
  33. .width('100%')
  34. .height('20%')
  35. .backgroundColor('#999')
  36. .alignSelf(ItemAlign.Start)
  37. }
  38. }

 

3.2.3 HorizontalAlign.End
  1. @Entry
  2. @Component
  3. struct RelativeContainerPage {
  4. @State message: string = 'Hello World'
  5. build() {
  6. Row() {
  7. RelativeContainerView()
  8. }
  9. .height('100%')
  10. }
  11. }
  12. @Component
  13. struct RelativeContainerView {
  14. build() {
  15. RelativeContainer() {
  16. Text('Anchor')
  17. .backgroundColor('#aa67c8ff')
  18. .id('anchor')
  19. .width('40%')
  20. .textAlign(TextAlign.Center)
  21. .alignRules({
  22. center: { anchor: '__container__', align: VerticalAlign.Center},
  23. middle: {anchor: '__container__', align: HorizontalAlign.Center}
  24. })
  25. Column()
  26. .alignRules({
  27. right: {anchor: 'anchor', align: HorizontalAlign.End}
  28. })
  29. .width(12)
  30. .height('100%')
  31. .backgroundColor('#80ff0000').id('vLine')
  32. }
  33. .width('100%')
  34. .height('20%')
  35. .backgroundColor('#999')
  36. .alignSelf(ItemAlign.Start)
  37. }
  38. }

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

3.2.4 VerticalAlign.Top
  1. @Entry
  2. @Component
  3. struct RelativeContainerPage {
  4. @State message: string = 'Hello World'
  5. build() {
  6. Row() {
  7. RelativeContainerView()
  8. }
  9. .height('100%')
  10. }
  11. }
  12. @Component
  13. struct RelativeContainerView {
  14. build() {
  15. RelativeContainer() {
  16. Text('Anchor')
  17. .backgroundColor('#aa67c8ff')
  18. .id('anchor')
  19. .width('40%')
  20. .textAlign(TextAlign.Center)
  21. .height(48)
  22. .alignRules({
  23. center: { anchor: '__container__', align: VerticalAlign.Center },
  24. middle: { anchor: '__container__', align: HorizontalAlign.Center }
  25. })
  26. Column()
  27. .alignRules({
  28. top: { anchor: 'anchor', align: VerticalAlign.Top }
  29. })
  30. .width('100%')
  31. .height(12)
  32. .backgroundColor('#80ff0000')
  33. .id('vLine')
  34. }
  35. .width('100%')
  36. .height('20%')
  37. .backgroundColor('#999')
  38. .alignSelf(ItemAlign.Start)
  39. }
  40. }

3.2.5 VerticalAlign.Center
  1. @Entry
  2. @Component
  3. struct RelativeContainerPage {
  4. @State message: string = 'Hello World'
  5. build() {
  6. Row() {
  7. RelativeContainerView()
  8. }
  9. .height('100%')
  10. }
  11. }
  12. @Component
  13. struct RelativeContainerView {
  14. build() {
  15. RelativeContainer() {
  16. Text('Anchor')
  17. .backgroundColor('#aa67c8ff')
  18. .id('anchor')
  19. .width('40%')
  20. .textAlign(TextAlign.Center)
  21. .height(48)
  22. .alignRules({
  23. center: { anchor: '__container__', align: VerticalAlign.Center },
  24. middle: { anchor: '__container__', align: HorizontalAlign.Center }
  25. })
  26. Column()
  27. .alignRules({
  28. center: { anchor: 'anchor', align: VerticalAlign.Center }
  29. })
  30. .width('100%')
  31. .height(12)
  32. .backgroundColor('#80ff0000')
  33. .id('vLine')
  34. }
  35. .width('100%')
  36. .height('20%')
  37. .backgroundColor('#999')
  38. .alignSelf(ItemAlign.Start)
  39. }
  40. }

3.2.6 VerticalAlign.Bottom
  1. @Entry
  2. @Component
  3. struct RelativeContainerPage {
  4. @State message: string = 'Hello World'
  5. build() {
  6. Row() {
  7. RelativeContainerView()
  8. }
  9. .height('100%')
  10. }
  11. }
  12. @Component
  13. struct RelativeContainerView {
  14. build() {
  15. RelativeContainer() {
  16. Text('Anchor')
  17. .backgroundColor('#aa67c8ff')
  18. .id('anchor')
  19. .width('40%')
  20. .textAlign(TextAlign.Center)
  21. .height(48)
  22. .alignRules({
  23. center: { anchor: '__container__', align: VerticalAlign.Center },
  24. middle: { anchor: '__container__', align: HorizontalAlign.Center }
  25. })
  26. Column()
  27. .alignRules({
  28. bottom: { anchor: 'anchor', align: VerticalAlign.Bottom }
  29. })
  30. .width('100%')
  31. .height(12)
  32. .backgroundColor('#80ff0000')
  33. .id('vLine')
  34. }
  35. .width('100%')
  36. .height('20%')
  37. .backgroundColor('#999')
  38. .alignSelf(ItemAlign.Start)
  39. }
  40. }

4、场景实例

        相对布局内的子元素相对灵活,只要在RelativeContainer容器内,均可以通过alignRules进行相应的位置移动。

  1. @Entry
  2. @Component
  3. struct RelativeLayoutSample {
  4. build() {
  5. Column() {
  6. RelativeContainer() {
  7. Row()
  8. .width(100)
  9. .height(100)
  10. .backgroundColor('#FF3333')
  11. .alignRules({
  12. top: { anchor: '__container__', align: VerticalAlign.Top }, //以父容器为锚点,竖直方向顶头对齐
  13. middle: { anchor: '__container__', align: HorizontalAlign.Center } //以父容器为锚点,水平方向居中对齐
  14. })
  15. .id('row1') //设置锚点为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组件为锚点,竖直方向低端对齐
  22. left: { anchor: 'row1', align: HorizontalAlign.Start } //以row1组件为锚点,水平方向开头对齐
  23. })
  24. .id('row2') //设置锚点为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') //设置锚点为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') //设置锚点为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') //设置锚点为row5
  51. }
  52. .width(300).height(300)
  53. .backgroundColor("#67c8ff")
  54. .border({ width: 2, color: '#6699FF' })
  55. }
  56. .justifyContent(FlexAlign.Start)
  57. .height('100%').width('100%').margin({ left: 0, right: 0 })
  58. }
  59. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/225242
推荐阅读
相关标签
  

闽ICP备14008679号