可以通过setWindowBackgroundColor实现,示例代码如下:
EntryAbility.ets
export default class EntryAbility extends UIAbility {
onWindowStageCreate(windowStage: window.WindowStage): void {
AppStorage.setOrCreate("windowStage",windowStage
) //保存windowStage
}
}
Index.ets
import window from '@ohos.window';
@Entry
@Component
struct Index {
build() {
Row() {
Column() {
Button('show dialog').onClick(() => {
let windowStage_: window.WindowStage = AppStorage.get('windowStage') as window.WindowStage;
windowStage_.createSubWindow('subWindow', (err, win) => { //创建透明子窗口并打开
win.setUIContent('pages/SubPage');
win.showWindow();
})
})
}
.width('100%')
}
.height('100%')
.backgroundColor(Color.Pink)
}
}
SubPage.ets
import window from '@ohos.window';
@Entry
@Component
struct SubPage {
@State message: string = 'this is a dialog page';
aboutToAppear() {
window.findWindow('subWindow').setWindowBackgroundColor("#00000000") //设置子窗口背景透明
}
onBackPress() { //关闭子窗口
window.findWindow('subWindow').destroyWindow().then((res) => {
console.log('destroyWindow success')
}).catch(() => {
console.log('destroyWindow fail')
})
return true
}
build() {
Row() {
Column() {
Text(this.message)
.fontSize(16)
.fontWeight(FontWeight.Bold)
.width(200)
.height(150)
.backgroundColor(Color.White)
}
.width('100%')
}
.backgroundColor("#32ffffff")
.height('100%')
}
}