背景設(shè)置

2024-01-22 16:08 更新

設(shè)置組件的背景樣式。

說明

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

屬性

名稱

參數(shù)類型

描述

backgroundColor

ResourceColor

設(shè)置組件的背景色。

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

backgroundImage

src: ResourceStr,

repeat?: ImageRepeat

src:圖片地址,支持網(wǎng)絡(luò)圖片資源和本地圖片資源地址(不支持svg類型的圖片)。

repeat:設(shè)置背景圖片的重復(fù)樣式,默認(rèn)不重復(fù)。當(dāng)設(shè)置的背景圖片為透明底色圖片,且同時設(shè)置了backgroundColor時,二者疊加顯示,背景顏色在最底部。

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

backgroundImageSize

{

width?: Length,

height?: Length

} | ImageSize

設(shè)置背景圖像的高度和寬度。當(dāng)輸入為{width: Length, height: Length}對象時,如果只設(shè)置一個屬性,則第二個屬性保持圖片原始寬高比進(jìn)行調(diào)整。默認(rèn)保持原圖的比例不變。

width和height取值范圍: [0, +∞)

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

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

說明:

設(shè)置為小于0的值時,按值為0顯示。當(dāng)設(shè)置了height未設(shè)置width時,width根據(jù)圖片原始寬高比進(jìn)行調(diào)整。

backgroundImagePosition

Position | Alignment

設(shè)置背景圖在組件中顯示位置,即相對于組件左上角的坐標(biāo)。

默認(rèn)值:

{

x: 0,

y: 0

}

x和y值設(shè)置百分比時,偏移量是相對組件自身寬高計(jì)算的。

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

示例

  1. // xxx.ets
  2. @Entry
  3. @Component
  4. struct BackgroundExample {
  5. build() {
  6. Column({ space: 5 }) {
  7. Text('background color').fontSize(9).width('90%').fontColor(0xCCCCCC)
  8. Row().width('90%').height(50).backgroundColor(0xE5E5E5).border({ width: 1 })
  9. Text('background image repeat along X').fontSize(9).width('90%').fontColor(0xCCCCCC)
  10. Row()
  11. .backgroundImage('/comment/bg.jpg', ImageRepeat.X)
  12. .backgroundImageSize({ width: '250px', height: '140px' })
  13. .width('90%')
  14. .height(70)
  15. .border({ width: 1 })
  16. Text('background image repeat along Y').fontSize(9).width('90%').fontColor(0xCCCCCC)
  17. Row()
  18. .backgroundImage('/comment/bg.jpg', ImageRepeat.Y)
  19. .backgroundImageSize({ width: '500px', height: '120px' })
  20. .width('90%')
  21. .height(100)
  22. .border({ width: 1 })
  23. Text('background image size').fontSize(9).width('90%').fontColor(0xCCCCCC)
  24. Row()
  25. .width('90%').height(150)
  26. .backgroundImage('/comment/bg.jpg', ImageRepeat.NoRepeat)
  27. .backgroundImageSize({ width: 1000, height: 500 })
  28. .border({ width: 1 })
  29. Text('background fill the box(Cover)').fontSize(9).width('90%').fontColor(0xCCCCCC)
  30. // 不保證圖片完整的情況下占滿盒子
  31. Row()
  32. .width(200)
  33. .height(50)
  34. .backgroundImage('/comment/bg.jpg', ImageRepeat.NoRepeat)
  35. .backgroundImageSize(ImageSize.Cover)
  36. .border({ width: 1 })
  37. Text('background fill the box(Contain)').fontSize(9).width('90%').fontColor(0xCCCCCC)
  38. // 保證圖片完整的情況下放到最大
  39. Row()
  40. .width(200)
  41. .height(50)
  42. .backgroundImage('/comment/bg.jpg', ImageRepeat.NoRepeat)
  43. .backgroundImageSize(ImageSize.Contain)
  44. .border({ width: 1 })
  45. Text('background image position').fontSize(9).width('90%').fontColor(0xCCCCCC)
  46. Row()
  47. .width(100)
  48. .height(50)
  49. .backgroundImage('/comment/bg.jpg', ImageRepeat.NoRepeat)
  50. .backgroundImageSize({ width: 1000, height: 560 })
  51. .backgroundImagePosition({ x: -500, y: -300 })
  52. .border({ width: 1 })
  53. }
  54. .width('100%').height('100%').padding({ top: 5 })
  55. }
  56. }

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

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號