彈性方式布局子組件的容器組件

2024-01-22 17:49 更新
說明
  • 該組件從API Version 7開始支持。后續(xù)版本如有新增內(nèi)容,則采用上角標單獨標記該內(nèi)容的起始版本。
  • Flex組件在渲染時存在二次布局過程,因此在對性能有嚴格要求的場景下建議使用ColumnRow代替。
  • Flex組件主軸默認不設(shè)置時撐滿父容器,Column、Row組件主軸不設(shè)置時默認是跟隨子節(jié)點大小。

權(quán)限列表

子組件

可以包含子組件。

接口

Flex(value?: { direction?: FlexDirection, wrap?: FlexWrap, justifyContent?: FlexAlign, alignItems?: ItemAlign, alignContent?: FlexAlign })

標準Flex布局容器。詳細指導請參考彈性布局

從API version 9開始,該接口支持在ArkTS卡片中使用。

參數(shù):

參數(shù)名

參數(shù)類型

必填

默認值

參數(shù)描述

direction

FlexDirection

FlexDirection.Row

子組件在Flex容器上排列的方向,即主軸的方向。

wrap

FlexWrap

FlexWrap.NoWrap

Flex容器是單行/列還是多行/列排列。

說明:

在多行布局時,通過交叉軸方向,確認新行堆疊方向。

justifyContent

FlexAlign

FlexAlign.Start

所有子組件在Flex容器主軸上的對齊格式。

alignItems

ItemAlign

ItemAlign.Start

所有子組件在Flex容器交叉軸上的對齊格式。

alignContent

FlexAlign

FlexAlign.Start

交叉軸中有額外的空間時,多行內(nèi)容的對齊方式。僅在wrap為Wrap或WrapReverse下生效。

示例

示例1

  1. // xxx.ets
  2. @Entry
  3. @Component
  4. struct FlexExample1 {
  5. build() {
  6. Column() {
  7. Column({ space: 5 }) {
  8. Text('direction:Row').fontSize(9).fontColor(0xCCCCCC).width('90%')
  9. Flex({ direction: FlexDirection.Row }) { // 子組件在容器主軸上行布局
  10. Text('1').width('20%').height(50).backgroundColor(0xF5DEB3)
  11. Text('2').width('20%').height(50).backgroundColor(0xD2B48C)
  12. Text('3').width('20%').height(50).backgroundColor(0xF5DEB3)
  13. Text('4').width('20%').height(50).backgroundColor(0xD2B48C)
  14. }
  15. .height(70)
  16. .width('90%')
  17. .padding(10)
  18. .backgroundColor(0xAFEEEE)
  19. Text('direction:RowReverse').fontSize(9).fontColor(0xCCCCCC).width('90%')
  20. Flex({ direction: FlexDirection.RowReverse }) { // 子組件在容器主軸上反向行布局
  21. Text('1').width('20%').height(50).backgroundColor(0xF5DEB3)
  22. Text('2').width('20%').height(50).backgroundColor(0xD2B48C)
  23. Text('3').width('20%').height(50).backgroundColor(0xF5DEB3)
  24. Text('4').width('20%').height(50).backgroundColor(0xD2B48C)
  25. }
  26. .height(70)
  27. .width('90%')
  28. .padding(10)
  29. .backgroundColor(0xAFEEEE)
  30. Text('direction:Column').fontSize(9).fontColor(0xCCCCCC).width('90%')
  31. Flex({ direction: FlexDirection.Column }) { // 子組件在容器主軸上列布局
  32. Text('1').width('100%').height(40).backgroundColor(0xF5DEB3)
  33. Text('2').width('100%').height(40).backgroundColor(0xD2B48C)
  34. Text('3').width('100%').height(40).backgroundColor(0xF5DEB3)
  35. Text('4').width('100%').height(40).backgroundColor(0xD2B48C)
  36. }
  37. .height(160)
  38. .width('90%')
  39. .padding(10)
  40. .backgroundColor(0xAFEEEE)
  41. Text('direction:ColumnReverse').fontSize(9).fontColor(0xCCCCCC).width('90%')
  42. Flex({ direction: FlexDirection.ColumnReverse }) { // 子組件在容器主軸上反向列布局
  43. Text('1').width('100%').height(40).backgroundColor(0xF5DEB3)
  44. Text('2').width('100%').height(40).backgroundColor(0xD2B48C)
  45. Text('3').width('100%').height(40).backgroundColor(0xF5DEB3)
  46. Text('4').width('100%').height(40).backgroundColor(0xD2B48C)
  47. }
  48. .height(160)
  49. .width('90%')
  50. .padding(10)
  51. .backgroundColor(0xAFEEEE)
  52. }.width('100%').margin({ top: 5 })
  53. }.width('100%')
  54. }
  55. }

示例2

  1. // xxx.ets
  2. @Entry
  3. @Component
  4. struct FlexExample2 {
  5. build() {
  6. Column() {
  7. Column({ space: 5 }) {
  8. Text('Wrap').fontSize(9).fontColor(0xCCCCCC).width('90%')
  9. Flex({ wrap: FlexWrap.Wrap }) { // 子組件多行布局
  10. Text('1').width('50%').height(50).backgroundColor(0xF5DEB3)
  11. Text('2').width('50%').height(50).backgroundColor(0xD2B48C)
  12. Text('3').width('50%').height(50).backgroundColor(0xD2B48C)
  13. }
  14. .width('90%')
  15. .padding(10)
  16. .backgroundColor(0xAFEEEE)
  17. Text('NoWrap').fontSize(9).fontColor(0xCCCCCC).width('90%')
  18. Flex({ wrap: FlexWrap.NoWrap }) { // 子組件單行布局
  19. Text('1').width('50%').height(50).backgroundColor(0xF5DEB3)
  20. Text('2').width('50%').height(50).backgroundColor(0xD2B48C)
  21. Text('3').width('50%').height(50).backgroundColor(0xF5DEB3)
  22. }
  23. .width('90%')
  24. .padding(10)
  25. .backgroundColor(0xAFEEEE)
  26. Text('WrapReverse').fontSize(9).fontColor(0xCCCCCC).width('90%')
  27. Flex({ wrap: FlexWrap.WrapReverse , direction:FlexDirection.Row }) { // 子組件反向多行布局
  28. Text('1').width('50%').height(50).backgroundColor(0xF5DEB3)
  29. Text('2').width('50%').height(50).backgroundColor(0xD2B48C)
  30. Text('3').width('50%').height(50).backgroundColor(0xD2B48C)
  31. }
  32. .width('90%')
  33. .height(120)
  34. .padding(10)
  35. .backgroundColor(0xAFEEEE)
  36. }.width('100%').margin({ top: 5 })
  37. }.width('100%')
  38. }
  39. }

示例3

  1. // xxx.ets
  2. @Component
  3. struct JustifyContentFlex {
  4. justifyContent : number = 0;
  5. build() {
  6. Flex({ justifyContent: this.justifyContent }) {
  7. Text('1').width('20%').height(50).backgroundColor(0xF5DEB3)
  8. Text('2').width('20%').height(50).backgroundColor(0xD2B48C)
  9. Text('3').width('20%').height(50).backgroundColor(0xF5DEB3)
  10. }
  11. .width('90%')
  12. .padding(10)
  13. .backgroundColor(0xAFEEEE)
  14. }
  15. }
  16. @Entry
  17. @Component
  18. struct FlexExample3 {
  19. build() {
  20. Column() {
  21. Column({ space: 5 }) {
  22. Text('justifyContent:Start').fontSize(9).fontColor(0xCCCCCC).width('90%')
  23. JustifyContentFlex({ justifyContent: FlexAlign.Start }) // 子組件在容器主軸上首端對齊
  24. Text('justifyContent:Center').fontSize(9).fontColor(0xCCCCCC).width('90%')
  25. JustifyContentFlex({ justifyContent: FlexAlign.Center }) // 子組件在容器主軸上居中對齊
  26. Text('justifyContent:End').fontSize(9).fontColor(0xCCCCCC).width('90%')
  27. JustifyContentFlex({ justifyContent: FlexAlign.End }) // 子組件在容器主軸上尾端對齊
  28. Text('justifyContent:SpaceBetween').fontSize(9).fontColor(0xCCCCCC).width('90%')
  29. JustifyContentFlex({ justifyContent: FlexAlign.SpaceBetween }) // 子組件在容器主軸上均分容器布局,第一個子組件與行首對齊,最后一個子組件與行尾對齊。
  30. Text('justifyContent:SpaceAround').fontSize(9).fontColor(0xCCCCCC).width('90%')
  31. JustifyContentFlex({ justifyContent: FlexAlign.SpaceAround }) // 子組件在容器主軸上均分容器布局,第一個子組件到行首的距離和最后一個子組件到行尾的距離是相鄰子組件之間距離的一半。
  32. Text('justifyContent:SpaceEvenly').fontSize(9).fontColor(0xCCCCCC).width('90%')
  33. JustifyContentFlex({ justifyContent: FlexAlign.SpaceEvenly }) // 子組件在容器主軸上均分容器布局,子組件之間的距離與第一子組件到行首、最后一個子組件到行尾的距離相等
  34. }.width('100%').margin({ top: 5 })
  35. }.width('100%')
  36. }
  37. }

示例4

  1. // xxx.ets
  2. @Component
  3. struct AlignItemsFlex {
  4. alignItems : number = 0
  5. build() {
  6. Flex({ alignItems: this.alignItems }) {
  7. Text('1').width('33%').height(30).backgroundColor(0xF5DEB3)
  8. Text('2').width('33%').height(40).backgroundColor(0xD2B48C)
  9. Text('3').width('33%').height(50).backgroundColor(0xF5DEB3)
  10. }
  11. .size({width: '90%', height: 80})
  12. .padding(10)
  13. .backgroundColor(0xAFEEEE)
  14. }
  15. }
  16. @Entry
  17. @Component
  18. struct FlexExample4 {
  19. build() {
  20. Column() {
  21. Column({ space: 5 }) {
  22. Text('alignItems:Auto').fontSize(9).fontColor(0xCCCCCC).width('90%')
  23. AlignItemsFlex({ alignItems: ItemAlign.Auto }) // 子組件在容器交叉軸上首部對齊
  24. Text('alignItems:Start').fontSize(9).fontColor(0xCCCCCC).width('90%')
  25. AlignItemsFlex({ alignItems: ItemAlign.Start }) // 子組件在容器交叉軸上首部對齊
  26. Text('alignItems:Center').fontSize(9).fontColor(0xCCCCCC).width('90%')
  27. AlignItemsFlex({ alignItems: ItemAlign.Center }) // 子組件在容器交叉軸上居中對齊
  28. Text('alignItems:End').fontSize(9).fontColor(0xCCCCCC).width('90%')
  29. AlignItemsFlex({ alignItems: ItemAlign.End }) // 子組件在容器交叉軸上尾部對齊
  30. Text('alignItems:Stretch').fontSize(9).fontColor(0xCCCCCC).width('90%')
  31. AlignItemsFlex({ alignItems: ItemAlign.Stretch }) // 子組件在容器交叉軸上拉伸填充
  32. Text('alignItems:Baseline').fontSize(9).fontColor(0xCCCCCC).width('90%')
  33. AlignItemsFlex({ alignItems: ItemAlign.Baseline }) // 子組件在容器交叉軸上與文本基線對齊
  34. }.width('100%').margin({ top: 5 })
  35. }.width('100%')
  36. }
  37. }

示例5

  1. // xxx.ets
  2. @Component
  3. struct AlignContentFlex {
  4. alignContent: number
  5. build() {
  6. Flex({ wrap: FlexWrap.Wrap, alignContent: this.alignContent }) {
  7. Text('1').width('50%').height(20).backgroundColor(0xF5DEB3)
  8. Text('2').width('50%').height(20).backgroundColor(0xD2B48C)
  9. Text('3').width('50%').height(20).backgroundColor(0xD2B48C)
  10. }
  11. .size({ width: '90%', height: 90 })
  12. .padding(10)
  13. .backgroundColor(0xAFEEEE)
  14. }
  15. }
  16. @Entry
  17. @Component
  18. struct FlexExample5 {
  19. build() {
  20. Column() {
  21. Column({ space: 5 }) {
  22. Text('alignContent:Start').fontSize(9).fontColor(0xCCCCCC).width('90%')
  23. AlignContentFlex({ alignContent: FlexAlign.Start }) // 多行布局下子組件首部對齊
  24. Text('alignContent:Center').fontSize(9).fontColor(0xCCCCCC).width('90%')
  25. AlignContentFlex({ alignContent: FlexAlign.Center }) // 多行布局下子組件居中對齊
  26. Text('alignContent:End').fontSize(9).fontColor(0xCCCCCC).width('90%')
  27. AlignContentFlex({ alignContent: FlexAlign.End }) // 多行布局下子組件尾部對齊
  28. Text('alignContent:SpaceBetween').fontSize(9).fontColor(0xCCCCCC).width('90%')
  29. AlignContentFlex({ alignContent: FlexAlign.SpaceBetween }) // 多行布局下第一行子組件與列首對齊,最后一行子組件與列尾對齊
  30. Text('alignContent:SpaceAround').fontSize(9).fontColor(0xCCCCCC).width('90%')
  31. AlignContentFlex({ alignContent: FlexAlign.SpaceAround }) // 多行布局下第一行子組件到列首的距離和最后一行子組件到列尾的距離是相鄰行之間距離的一半
  32. Text('alignContent:SpaceEvenly').fontSize(9).fontColor(0xCCCCCC).width('90%')
  33. Flex({
  34. wrap: FlexWrap.Wrap,
  35. alignContent: FlexAlign.SpaceEvenly
  36. }) { // 多行布局下相鄰行之間的距離與第一行子組件到列首的距離、最后一行子組件到列尾的距離完全一樣
  37. Text('1').width('50%').height(20).backgroundColor(0xF5DEB3)
  38. Text('2').width('50%').height(20).backgroundColor(0xD2B48C)
  39. Text('3').width('50%').height(20).backgroundColor(0xF5DEB3)
  40. Text('4').width('50%').height(20).backgroundColor(0xD2B48C)
  41. Text('5').width('50%').height(20).backgroundColor(0xF5DEB3)
  42. }
  43. .size({ width: '90%', height: 100 })
  44. .padding({ left: 10, right: 10 })
  45. .backgroundColor(0xAFEEEE)
  46. }.width('100%').margin({ top: 5 })
  47. }.width('100%')
  48. }
  49. }

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

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號