【Harmony OS】【ARK UI】自定义弹窗
自定义弹窗
1.在日常开发当中自定义弹窗会经常用到,之前有遇到想自定义弹窗位置不知该如何去设置的情况(如:相对底部/顶部多少距离)今天就来说一说自定义弹窗的一些属性和用法
官方文档地址:
文档中心
2.代码实现:
@CustomDialogstruct 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@Componentstruct 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 }) }}
3.运行效果:
需要注意的是:
弹窗往上移取负值,向下取正值
欲了解更多更全技术文章,欢迎访问https://developer.huawei.com/consumer/cn/forum/?ha_source=zzh