@Entry
@Component
struct Index {
dialogController: CustomDialogController = new CustomDialogController(
{
builder: CustomDialogComponent(),
backgroundBlurStyle:BlurStyle.NONE,
offset:{dx:0,dy:-50}
});
build() {
Column({ space: 5 }) {
Button('打开弹窗')
.height(40)
.onClick(() => {
this.dialogController.open();
});
}
.justifyContent(FlexAlign.Center)
.padding(20)
.height('100%')
.width('100%');
}
}
// 自定义弹窗组件
@CustomDialog
struct CustomDialogComponent {
controller?: CustomDialogController;
@State showDialog: boolean = false;
build() {
Column() {
Button('半透明')
.height(40)
.margin({top:150,bottom:60})
.onClick(() => {
this.showDialog = !this.showDialog;
});
Button('关闭')
.height(40)
.backgroundColor('#0a59f7')
.onClick(() => {
// 关闭弹窗
this.controller?.close();
})
}
.justifyContent(FlexAlign.SpaceEvenly)
.width('100%')
.height(400)
// 7F表示50%透明度
.backgroundColor(this.showDialog ? '#7FFFFFFF' : '#FFFFFF')
}
} 效果图如下:

@Entry
@Component
struct OpacityExample {
@State IsOpacity: Boolean = false;
build() {
Column({ space: 5 }) {
Button('改变状态')
.onClick(() => {
this.IsOpacity = !this.IsOpacity;
});
}
.expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.TOP, SafeAreaEdge.BOTTOM])
.height('100%')
.width('100%')
.backgroundColor('#f1f3f5')
.opacity(this.IsOpacity ? 0.5 : 1);
}
} 效果图如下:

名称 | 值 | 说明 |
|---|---|---|
STANDARD | 0 | 标准模式的NavDestination |
DIALOG | 1 | 默认透明,进出页面栈不影响下层NavDestination的生命周期 |
示例代码如下:
@Entry
@Component
struct TransparentPage {
@Provide('NavPathStack') pageStack: NavPathStack = new NavPathStack();
@Builder
PagesMap(name: string) {
if (name === 'DialogPage') {
DialogPage();
}
}
build() {
Navigation(this.pageStack) {
Button('Push DialogPage')
.margin(20)
.width('80%')
.onClick(() => {
this.pageStack.pushPathByName('DialogPage', '');
});
}.mode(NavigationMode.Stack)
.title('Main')
.navDestination(this.PagesMap);
}
}
@Component
export struct DialogPage {
@Consume('NavPathStack') pageStack: NavPathStack;
build() {
NavDestination() {
Stack({ alignContent: Alignment.Center }) {
Column() {
Text('Dialog NavDestination')
.fontSize(20)
.margin({ bottom: 100 });
Button('Close')
.onClick(() => {
this.pageStack.pop();
})
.width('30%');
}
.justifyContent(FlexAlign.Center)
.borderRadius(10)
.height('30%')
.width('80%');
}
.height('100%')
.width('100%');
}
.mode(NavDestinationMode.DIALOG)
.hideTitleBar(true)
.expandSafeArea([SafeAreaType.KEYBOARD]);
}
} 效果图如下:

页面A代码实现:
import window from '@ohos.window';
@Entry
@Component
struct APage {
build() {
Row() {
Column() {
Button('pageA').onClick(() => {
let windowStage_: window.WindowStage = AppStorage.get('windowStage') as window.WindowStage;
// 创建透明子窗口并打开
windowStage_.createSubWindow('subWindow', (err, win) => {
console.error(`err: ${err}`);
win.setUIContent('pages/BPage');
win.showWindow();
});
});
}
.width('100%');
}
.height('100%')
.backgroundColor(Color.White);
}
} 页面B代码实现:
import window from '@ohos.window';
@Entry
@Component
struct BPage {
message: string = 'BPage';
aboutToAppear() {
// 设置子窗口背景透明
window.findWindow('subWindow').setWindowBackgroundColor('#00000000');
}
onBackPress() { // 关闭子窗口
window.findWindow('subWindow').destroyWindow().then((res) => {
console.info(`destroyWindow success res: ${res}`);
}).catch(() => {
console.error('destroyWindow fail');
});
return true;
}
build() {
Row() {
Column() {
Text(this.message)
.fontSize(50)
.fontWeight(FontWeight.Bold);
}
.width('100%');
}
// 组件背景,可以修改为#00000000透明
.backgroundColor('#80FFFFFF')
.height('100%');
}
} EntryAbility.ets关键代码:
onWindowStageCreate(windowStage: window.WindowStage): void {
// Main window is created, set main page for this ability
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
// 全局存储窗口变量
AppStorage.setOrCreate('windowStage', windowStage);
windowStage.loadContent('pages/Index', (err) => {
if (err.code) {
hilog.error(DOMAIN, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err));
return;
}
hilog.info(DOMAIN, 'testTag', 'Succeeded in loading the content.');
});
} 效果预览:

可通过给组件添加backgroundColor属性,使用rgba来实现透明背景,具体可参考文档:ResourceColor。
参考demo:
@Entry
@Component
struct ScrollExample {
scroller: Scroller = new Scroller();
private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
@State alpha: string = '';
build() {
Column() {
Row()
.width('100%')
.height(30)
.backgroundColor(this.alpha);
Scroll(this.scroller) {
Column() {
ForEach(this.arr, (item: string) => {
Text(item.toString())
.width('90%')
.height(150)
.backgroundColor('#f1f3f5')
.borderRadius(15)
.fontSize(16)
.textAlign(TextAlign.Center)
.margin({ top: 10 });
}, (item: string) => item);
}.width('100%');
}
.width('100%')
// 滚动事件回调,Scroll滚动前触发。
.onWillScroll((xOffset: number, yOffset: number) => {
console.info(`xOffset: ${xOffset}, yOffset: ${yOffset}`);
let offset = this.scroller.currentOffset().yOffset;
if (offset >= 150) {
this.alpha = 'rgba(128, 128, 128, 0.80)';
} else if (offset <= 0) {
this.alpha = 'rgba(128, 128, 128, 0.60)';
} else {
this.alpha = 'rgba(128, 128, 128, 0.30)';
}
});
};
}
} 效果图如下:

Q:在元服务中通过setWindowBackgroundColor('#00000000')设置子窗口为透明色为什么不生效?
A:当前的元服务应用内所有子窗都会显示menubar,menubar会产生不透明背景,该效果为当前规格。
Q:编译时报错,提示“Argument of Type 'string|ColorMetrics' is not assignable to parameter of type 'string'”?
A:setWindowBackgroundColor函数从API9开始支持,但ColorMetrics入参类型从API18开始支持,请关注API接口变更情况。