位置設(shè)置

2024-01-22 12:26 更新

設(shè)置組件的對(duì)齊方式、布局方向和顯示位置。

說(shuō)明

從API Version 7開(kāi)始支持。后續(xù)版本如有新增內(nèi)容,則采用上角標(biāo)單獨(dú)標(biāo)記該內(nèi)容的起始版本。

屬性

名稱(chēng)

參數(shù)類(lèi)型

描述

align

Alignment

設(shè)置元素內(nèi)容在元素繪制區(qū)域內(nèi)的對(duì)齊方式。

默認(rèn)值:Alignment.Center

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

direction

Direction

設(shè)置元素水平方向的布局。

默認(rèn)值:Direction.Auto

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

position

Position

絕對(duì)定位,設(shè)置元素左上角相對(duì)于父容器左上角偏移位置。在布局容器中,設(shè)置該屬性不影響父容器布局,僅在繪制時(shí)進(jìn)行位置調(diào)整。

適用于置頂顯示、懸浮按鈕等組件在父容器中位置固定的場(chǎng)景。

markAnchor

Position

設(shè)置元素在位置定位時(shí)的錨點(diǎn),以元素左上角作為基準(zhǔn)點(diǎn)進(jìn)行偏移。通常配合position和offset屬性使用,單獨(dú)使用時(shí),效果類(lèi)似offset

默認(rèn)值:

{

x: 0,

y: 0

}

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

offset

Position

相對(duì)定位,設(shè)置元素相對(duì)于自身的偏移量。設(shè)置該屬性,不影響父容器布局,僅在繪制時(shí)進(jìn)行位置調(diào)整。

默認(rèn)值:

{

x: 0,

y: 0

}

alignRules9+

{

left?: { anchor: string, align: HorizontalAlign };

right?: { anchor: string, align: HorizontalAlign };

middle?: { anchor: string, align: HorizontalAlign };

top?: { anchor: string, align: VerticalAlign };

bottom?: { anchor: string, align: VerticalAlign };

center?: { anchor: string, align: VerticalAlign }

}

指定相對(duì)容器的對(duì)齊規(guī)則。

- left:設(shè)置左對(duì)齊參數(shù)。

- right:設(shè)置右對(duì)齊參數(shù)。

- middle:設(shè)置橫向居中對(duì)齊方式的參數(shù)。

- top:設(shè)置頂部對(duì)齊的參數(shù)。

- bottom:設(shè)置底部對(duì)齊的參數(shù)。

- center:設(shè)置縱向居中對(duì)齊方式的參數(shù)。

該接口支持在ArkTS卡片中使用。

說(shuō)明:

- anchor:設(shè)置作為錨點(diǎn)的組件的id值。

- align:設(shè)置相對(duì)于錨點(diǎn)組件的對(duì)齊方式。

示例

示例1

  1. // xxx.ets
  2. @Entry
  3. @Component
  4. struct PositionExample1 {
  5. build() {
  6. Column() {
  7. Column({ space: 10 }) {
  8. // 元素內(nèi)容<元素寬高,設(shè)置內(nèi)容在與元素內(nèi)的對(duì)齊方式
  9. Text('align').fontSize(9).fontColor(0xCCCCCC).width('90%')
  10. Stack() {
  11. Text('First show in bottom end').height('65%').backgroundColor(0xD2B48C)
  12. Text('Second show in bottom end').backgroundColor(0xF5DEB3).opacity(0.9)
  13. }.width('90%').height(50).margin({ top: 5 }).backgroundColor(0xFFE4C4)
  14. .align(Alignment.BottomEnd)
  15. Stack() {
  16. Text('top start')
  17. }.width('90%').height(50).margin({ top: 5 }).backgroundColor(0xFFE4C4)
  18. .align(Alignment.TopStart)
  19. // 父容器設(shè)置direction為Direction.Ltr,子元素從左到右排列
  20. Text('direction').fontSize(9).fontColor(0xCCCCCC).width('90%')
  21. Row() {
  22. Text('1').height(50).width('25%').fontSize(16).backgroundColor(0xF5DEB3)
  23. Text('2').height(50).width('25%').fontSize(16).backgroundColor(0xD2B48C)
  24. Text('3').height(50).width('25%').fontSize(16).backgroundColor(0xF5DEB3)
  25. Text('4').height(50).width('25%').fontSize(16).backgroundColor(0xD2B48C)
  26. }
  27. .width('90%')
  28. .direction(Direction.Ltr)
  29. // 父容器設(shè)置direction為Direction.Rtl,子元素從右到左排列
  30. Row() {
  31. Text('1').height(50).width('25%').fontSize(16).backgroundColor(0xF5DEB3).textAlign(TextAlign.End)
  32. Text('2').height(50).width('25%').fontSize(16).backgroundColor(0xD2B48C).textAlign(TextAlign.End)
  33. Text('3').height(50).width('25%').fontSize(16).backgroundColor(0xF5DEB3).textAlign(TextAlign.End)
  34. Text('4').height(50).width('25%').fontSize(16).backgroundColor(0xD2B48C).textAlign(TextAlign.End)
  35. }
  36. .width('90%')
  37. .direction(Direction.Rtl)
  38. }
  39. }
  40. .width('100%').margin({ top: 5 })
  41. }
  42. }

示例2

  1. // xxx.ets
  2. @Entry
  3. @Component
  4. struct PositionExample2 {
  5. build() {
  6. Column({ space: 20 }) {
  7. // 設(shè)置子組件左上角相對(duì)于父組件左上角的偏移位置
  8. Text('position').fontSize(12).fontColor(0xCCCCCC).width('90%')
  9. Row() {
  10. Text('1').size({ width: '30%', height: '50' }).backgroundColor(0xdeb887).border({ width: 1 }).fontSize(16)
  11. Text('2 position(30, 10)')
  12. .size({ width: '60%', height: '30' })
  13. .backgroundColor(0xbbb2cb)
  14. .border({ width: 1 })
  15. .fontSize(16)
  16. .align(Alignment.Start)
  17. .position({ x: 30, y: 10 })
  18. Text('3').size({ width: '45%', height: '50' }).backgroundColor(0xdeb887).border({ width: 1 }).fontSize(16)
  19. Text('4 position(50%, 70%)')
  20. .size({ width: '50%', height: '50' })
  21. .backgroundColor(0xbbb2cb)
  22. .border({ width: 1 })
  23. .fontSize(16)
  24. .position({ x: '50%', y: '70%' })
  25. }.width('90%').height(100).border({ width: 1, style: BorderStyle.Dashed })
  26. // 相對(duì)于起點(diǎn)偏移,其中x為最終定位點(diǎn)距離起點(diǎn)水平方向間距,x>0往左,反之向右。
  27. // y為最終定位點(diǎn)距離起點(diǎn)垂直方向間距,y>0向上,反之向下
  28. Text('markAnchor').fontSize(12).fontColor(0xCCCCCC).width('90%')
  29. Stack({ alignContent: Alignment.TopStart }) {
  30. Row()
  31. .size({ width: '100', height: '100' })
  32. .backgroundColor(0xdeb887)
  33. Text('text')
  34. .size({ width: 25, height: 25 })
  35. .backgroundColor(Color.Green)
  36. .markAnchor({ x: 25, y: 25 })
  37. Text('text')
  38. .size({ width: 25, height: 25 })
  39. .backgroundColor(Color.Green)
  40. .markAnchor({ x: -100, y: -25 })
  41. Text('text')
  42. .size({ width: 25, height: 25 })
  43. .backgroundColor(Color.Green)
  44. .markAnchor({ x: 25, y: -25 })
  45. }.margin({ top: 25 }).border({ width: 1, style: BorderStyle.Dashed })
  46. // 相對(duì)定位,x>0向右偏移,反之向左,y>0向下偏移,反之向上
  47. Text('offset').fontSize(12).fontColor(0xCCCCCC).width('90%')
  48. Row() {
  49. Text('1').size({ width: '15%', height: '50' }).backgroundColor(0xdeb887).border({ width: 1 }).fontSize(16)
  50. Text('2 offset(15, 30)')
  51. .size({ width: 120, height: '50' })
  52. .backgroundColor(0xbbb2cb)
  53. .border({ width: 1 })
  54. .fontSize(16)
  55. .align(Alignment.Start)
  56. .offset({ x: 15, y: 30 })
  57. Text('3').size({ width: '15%', height: '50' }).backgroundColor(0xdeb887).border({ width: 1 }).fontSize(16)
  58. Text('4 offset(-10%, 20%)')
  59. .size({ width: 100, height: '50' })
  60. .backgroundColor(0xbbb2cb)
  61. .border({ width: 1 })
  62. .fontSize(16)
  63. .offset({ x: '-5%', y: '20%' })
  64. }.width('90%').height(100).border({ width: 1, style: BorderStyle.Dashed })
  65. }
  66. .width('100%').margin({ top: 25 })
  67. }
  68. }

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

掃描二維碼

下載編程獅App

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

編程獅公眾號(hào)