CustomDialog设置透明背景的解决方案如下:
@CustomDialog
struct CustomDialogContent {
controller: CustomDialogController;
build() {
Column() {
Button('关闭').onClick(() => {
this.controller.close();
})
.backgroundColor('#0a59f7');
}
.justifyContent(FlexAlign.Center)
.height('100%')
.width('100%');
}
}
@Entry
@Component
struct Index {
dialogController: CustomDialogController = new CustomDialogController({
builder: CustomDialogContent(),
// 设置弹窗背景色为透明
backgroundColor: Color.Transparent,
backgroundBlurStyle: BlurStyle.NONE
});
build() {
Row() {
Button('弹窗').onClick(() => {
this.dialogController.open();
})
.backgroundColor('#0a59f7')
.margin({ top: 100 });
}
.expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.TOP, SafeAreaEdge.BOTTOM])
.alignItems(VerticalAlign.Center)
.justifyContent(FlexAlign.Center)
.height('100%')
.width('100%')
.backgroundColor(0xF1F3F5);
}
} @CustomDialog
struct CustomDialogContent1 {
controller: CustomDialogController;
build() {
Column() {
Button('关闭').onClick(() => {
this.controller.close();
})
.backgroundColor('#0a59f7');
};
}
}
@Entry
@Component
struct Index1 {
dialogController: CustomDialogController = new CustomDialogController({
builder: CustomDialogContent1(),
customStyle: true, // 设置弹窗背景色为透明
});
build() {
Row() {
Button('弹窗').onClick(() => {
this.dialogController.open();
})
.backgroundColor('#0a59f7') // 按钮颜色
.margin({ top: 100 });
}
.expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.TOP, SafeAreaEdge.BOTTOM])
.alignItems(VerticalAlign.Center)
.justifyContent(FlexAlign.Center)
.height('100%')
.width('100%')
.backgroundColor(0xF1F3F5); // 主页面背景色
}
} @CustomDialog
struct CustomDialogContent2 {
controller: CustomDialogController;
build() {
Column() {
Button('关闭').onClick(() => {
this.controller.close();
})
.backgroundColor('#0a59f7');
};
}
}
@Entry
@Component
struct Index2 {
dialogController: CustomDialogController = new CustomDialogController({
builder: CustomDialogContent2(),
customStyle: true, // 设置弹窗背景色为透明
isModal: false
});
build() {
Row() {
Button('弹窗').onClick(() => {
this.dialogController.open();
})
.backgroundColor('#0a59f7')
.margin({ top: 100 });
}
.expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.TOP, SafeAreaEdge.BOTTOM])
.alignItems(VerticalAlign.Center)
.justifyContent(FlexAlign.Center)
.height('100%')
.width('100%')
.backgroundColor(0xF1F3F5);
}
} Q:如何修改自定义弹窗的背景色?同时怎么设定弹窗不点击遮罩就能消除?
A:可以使用自定义弹窗CustomDialogControllerOptions对象说明下的蒙层颜色属性maskColor修改弹窗背景色,是否允许点击遮障层退出属性autoCancel设定弹窗不点击遮罩就能消除。
Q:为什么使用maskColor:"0xB0000000"设置maskColor无效?
A:该写法不符合规范,因此失效,可通过maskColor:0xB0000000或maskColor:'#B0000000'这样的规范写法设置maskColor。
Q:为maskColor设置resource类型不生效。
A:maskColor当前只支持字符串颜色值,不支持$r()资源引用。
Q:CustomDialog设置透明背景失效的可能原因有哪些?
A:设置透明背景失效的可能原因如下:1.customStyle未设置为true(即未开启自定义样式);2.maskColor(自定义蒙层颜色)、backgroundColor(弹窗背板填充颜色)未设置透明;3.自定义弹窗内容构造器builder内容器背景未设置透明。相关属性配置可参考CustomDialogControllerOptions对象说明。
方案一、二、三都是通过自定义CustomDialogController类方法实现透明背景,但实现方法略有不同,因此应用场景也不同,常见场景如下表格:
方案 | 特点 | 适用场景 |
|---|---|---|
方案一 | 弹窗背板填充和模糊材质固定 | 消息提示、操作确认、图片预览 |
方案二 | 不能自定义弹窗容器样式 | 系统消息提示、应用权限请求、软件错误提示 |
方案三 | 背景完全透明 | 悬浮窗效果、评论弹窗 |