HarmonyOS 弹窗从底部弹出变成从中间弹出

使用文档中的自定义弹窗代码,样式变化

struct CustomDialogCall {
  cancel?: () => void
  confirm?: () => void
  controller: CustomDialogController
  build() {
    Column() {
      Text(getContext(this).resourceManager.getStringSync($r('app.string.call').id) + getContext(this).resourceManager.getStringSync($r('app.string.phone_number_call').id))
        .fontSize(16)
        .margin({ top: 20, bottom: 20 })
      Flex({ justifyContent: FlexAlign.SpaceAround }) {
        Button($r('app.string.cancel'))
          .onClick(() => {
            this.controller.close()
            if (this.cancel) {
              this.cancel()
            }
          }).backgroundColor(0xffffff).fontColor(Color.Black)
        Button($r('app.string.confirm'))
          .onClick(() => {
            this.controller.close()
            if (this.confirm) {
              this.confirm()
            }
          }).backgroundColor(0xffffff).fontColor(Color.Red)
      }.margin({ bottom: 10 })
    }
  }
}
HarmonyOS
3天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zbw_apple

需要设置alignment: DialogAlignment.Bottom,

@CustomDialog
struct CustomDialogExample {
  cancel?: () => void
  confirm?: () => void
  controller: CustomDialogController
  build() {
    Column() {
      Text(getContext(this).resourceManager.getStringSync($r('app.string.app_name').id) + getContext(this).resourceManager.getStringSync($r('app.string.reason').id))
        .fontSize(16)
        .margin({ top: 20, bottom: 20 })
      Flex({ justifyContent: FlexAlign.SpaceAround }) {
        Button($r('app.string.module_desc'))
          .onClick(() => {
            this.controller.close()
            if (this.cancel) {
              this.cancel()
            }
          }).backgroundColor(0xffffff).fontColor(Color.Black)
        Button($r('app.string.app_name'))
          .onClick(() => {
            this.controller.close()
            if (this.confirm) {
              this.confirm()
            }
          }).backgroundColor(0xffffff).fontColor(Color.Red)
      }.margin({ bottom: 10 })
    }
  }
}
@Entry
@Component
struct CustomDialogUser {
  @State textValue: string = ''
  @State inputValue: string = 'click me'
  dialogController: CustomDialogController | null = new CustomDialogController({
    builder: CustomDialogExample({
      cancel: ()=> { this.onCancel() },
      confirm: ()=> { this.onAccept() },

    }),
    cancel: this.exitApp,
    autoCancel: true,
    onWillDismiss:(dismissDialogAction: DismissDialogAction)=> {
      console.info("reason=" + JSON.stringify(dismissDialogAction.reason))
      console.log("dialog onWillDismiss")
      if (dismissDialogAction.reason == DismissReason.PRESS_BACK) {
        dismissDialogAction.dismiss()
      }
      if (dismissDialogAction.reason == DismissReason.TOUCH_OUTSIDE) {
        dismissDialogAction.dismiss()
      }
    },
    alignment: DialogAlignment.Bottom,
    offset: { dx: 0, dy: -20 },
    gridCount: 4,
    customStyle: false,
    cornerRadius: 10,
  })

  // 在自定义组件即将析构销毁时将dialogController置空
  aboutToDisappear() {
    this.dialogController = null // 将dialogController置空
  }

  onCancel() {
    console.info('Callback when the first button is clicked')
  }

  onAccept() {
    console.info('Callback when the second button is clicked')
  }

  exitApp() {
    console.info('Click the callback in the blank area')
  }
  build() {
    Column() {
      Button(this.inputValue)
        .onClick(() => {
          if (this.dialogController != null) {
            this.dialogController.open()
          }
        }).backgroundColor(0x317aff)
    }.width('100%').margin({ top: 5 })
  }
}
分享
微博
QQ
微信
回复
3天前
相关问题
HarmonyOS share弹窗无法弹出
56浏览 • 1回复 待解决
HarmonyOS 页面以弹窗的样式弹出
574浏览 • 1回复 待解决
HarmonyOS 首页多个弹窗按顺序弹出
42浏览 • 1回复 待解决
HarmonyOS 全局自定义弹窗无法弹出
52浏览 • 1回复 待解决
鸿蒙软键盘弹出后,页面底部的按钮
4163浏览 • 0回复 待解决