W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
設(shè)置組件的背景樣式。
從API Version 7開始支持。后續(xù)版本如有新增內(nèi)容,則采用上角標(biāo)單獨標(biāo)記該內(nèi)容的起始版本。
名稱 | 參數(shù)類型 | 描述 |
---|---|---|
backgroundColor | 設(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è)置一個屬性,則第二個屬性保持圖片原始寬高比進行調(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ù)圖片原始寬高比進行調(diào)整。 |
backgroundImagePosition | 設(shè)置背景圖在組件中顯示位置,即相對于組件左上角的坐標(biāo)。 默認(rèn)值: { x: 0, y: 0 } x和y值設(shè)置百分比時,偏移量是相對組件自身寬高計算的。 從API version 9開始,該接口支持在ArkTS卡片中使用。 |
- // xxx.ets
- @Entry
- @Component
- struct BackgroundExample {
- build() {
- Column({ space: 5 }) {
- Text('background color').fontSize(9).width('90%').fontColor(0xCCCCCC)
- Row().width('90%').height(50).backgroundColor(0xE5E5E5).border({ width: 1 })
- Text('background image repeat along X').fontSize(9).width('90%').fontColor(0xCCCCCC)
- Row()
- .backgroundImage('/comment/bg.jpg', ImageRepeat.X)
- .backgroundImageSize({ width: '250px', height: '140px' })
- .width('90%')
- .height(70)
- .border({ width: 1 })
- Text('background image repeat along Y').fontSize(9).width('90%').fontColor(0xCCCCCC)
- Row()
- .backgroundImage('/comment/bg.jpg', ImageRepeat.Y)
- .backgroundImageSize({ width: '500px', height: '120px' })
- .width('90%')
- .height(100)
- .border({ width: 1 })
- Text('background image size').fontSize(9).width('90%').fontColor(0xCCCCCC)
- Row()
- .width('90%').height(150)
- .backgroundImage('/comment/bg.jpg', ImageRepeat.NoRepeat)
- .backgroundImageSize({ width: 1000, height: 500 })
- .border({ width: 1 })
- Text('background fill the box(Cover)').fontSize(9).width('90%').fontColor(0xCCCCCC)
- // 不保證圖片完整的情況下占滿盒子
- Row()
- .width(200)
- .height(50)
- .backgroundImage('/comment/bg.jpg', ImageRepeat.NoRepeat)
- .backgroundImageSize(ImageSize.Cover)
- .border({ width: 1 })
- Text('background fill the box(Contain)').fontSize(9).width('90%').fontColor(0xCCCCCC)
- // 保證圖片完整的情況下放到最大
- Row()
- .width(200)
- .height(50)
- .backgroundImage('/comment/bg.jpg', ImageRepeat.NoRepeat)
- .backgroundImageSize(ImageSize.Contain)
- .border({ width: 1 })
- Text('background image position').fontSize(9).width('90%').fontColor(0xCCCCCC)
- Row()
- .width(100)
- .height(50)
- .backgroundImage('/comment/bg.jpg', ImageRepeat.NoRepeat)
- .backgroundImageSize({ width: 1000, height: 560 })
- .backgroundImagePosition({ x: -500, y: -300 })
- .border({ width: 1 })
- }
- .width('100%').height('100%').padding({ top: 5 })
- }
- }
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: