更新时间:2022-03-17 09:10:03浏览次数:706+次
以下介绍鸿蒙HarmonyOS技术之ARK UI自定义弹窗的实现,附实现的代码和效果截图。自定义弹窗在日常开发当中自定义弹窗会经常用到,之前有遇到想自定义弹窗位置不知该如何去设置的情况,如:相对底部/顶部多少距离,下面也用到自定义弹窗的一些属性和用法,如下图所示,有兴趣的可以参考鸿蒙官方文档中的“自定义弹窗”。
实现的代码
@CustomDialog
struct CustomDialogExample {
controller: CustomDialogController
cancel: () => void
confirm: () => void
build() {
Column() {
Text('弹窗标题:卸载软件').width('70%').fontSize(20).margin({ top: 10, bottom: 10 })
Image('comment/bg.jpg').width(80).height(80) //自定义弹窗图片
// Image($r('app.media.icon')).width(80).height(80)
Text('确定要卸载吗?').fontSize(16).margin({ bottom: 10 })
Flex({ justifyContent: FlexAlign.SpaceAround }) {
Button('取消')
.onClick(() => {
this.controller.close()
this.cancel()
}).backgroundColor(0xffffff).fontColor(Color.Black)
Button('确认')
.onClick(() => {
this.controller.close()
this.confirm()
}).backgroundColor(0xffffff).fontColor(Color.Red)
}.margin({ bottom: 10 })
}
}
}
@Entry
@Component
struct CustomDialogUser {
dialogController: CustomDialogController = new CustomDialogController({
builder: CustomDialogExample({ cancel: this.onCancel, confirm: this.onAccept }),
cancel: this.existApp,
autoCancel: true, //点击空白处退出弹窗 false不退出,true退出
alignment: DialogAlignment.Bottom , //垂直底部对齐
offset:{dx:0,dy:-30} //需要注意的是弹窗从底部往上移取负值
})
onCancel() {
console.info('点击取消按钮时的回调')
}
onAccept() {
console.info('单击确认按钮时的回调')
}
existApp() {
console.info('点击空白处的回调')
}
build() {
Column() {
Button('点击按钮触发弹窗')
.onClick(() => {
//open() 打开自定义弹窗
this.dialogController.open()
}).backgroundColor(0x317aff)
}.width('100%').margin({ top: 5 })
}
}
注意事项
需要注意的是:弹窗往上移取负值,向下取正值。
运行效果
相关资讯