HarmonyOS 鸿蒙Next关于自定义dialog的宽度设置问题

HarmonyOS 鸿蒙Next关于自定义dialog的宽度设置问题

@CustomDialog export struct WeDialog {}

我这边自定义了一个Dialog,但是我想设置这个Dialog的宽度,请问怎么设置,谢谢
2 回复

可以通过gridCount:弹窗宽度占栅格宽度的个数。最大栅格数为系统最大栅格数,在引入自定义弹窗时设置gridCount。

您可以参考如下代码:

@CustomDialog

struct CustomDialogExample {

  @Link textValue: string

  @Link inputValue: string

  dialogControllerTwo: CustomDialogController | null = new CustomDialogController({
    builder: CustomDialogExample,
    alignment: DialogAlignment.Bottom,
    offset: { dx: 0, dy: -25 } 
  })

  controller?: CustomDialogController

  cancel(): void = () => {
  }

  confirm(): void = () => {
  }

  build() {

    Column() {

      Text('Change text').fontSize(20).margin({ top: 10, bottom: 10 })

      TextInput({ placeholder: '', text: this.textValue }).height(60).width('90%')
        .onChange((value: string) => {
          this.textValue = value
        })

      Text('Whether to change a text?').fontSize(16).margin({ bottom: 10 })

      Flex({ justifyContent: FlexAlign.SpaceAround }) {

        Button('cancel')
          .onClick(() => {
            if (this.controller != undefined) {
              this.controller.close()
              this.cancel()
            }
          }).backgroundColor(0xffffff).fontColor(Color.Black)

        Button('confirm')
          .onClick(() => {
            if (this.controller != undefined) {
              this.inputValue = this.textValue
              this.controller.close()
              this.confirm()
            }
          }).backgroundColor(0xffffff).fontColor(Color.Red)
      }.margin({ bottom: 10 })
    }.borderRadius(10)

    // 如果需要使用border属性或cornerRadius属性,请和borderRadius属性一起使用。
  }
}

@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() },
      textValue: $textValue,
      inputValue: $inputValue
    }),
    cancel: this.exitApp,
    autoCancel: true,
    alignment: DialogAlignment.Bottom,
    offset: { dx: 0, dy: -20 },
    gridCount: 3,
    customStyle: false,
    cornerRadius: 10
  })

  // 在自定义组件即将析构销毁时将dialogControlle置空
  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 })
  }
}

可以参考如下链接: https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-methods-custom-dialog-box#customdialogcontroller

更多关于HarmonyOS 鸿蒙Next关于自定义dialog的宽度设置问题的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS(鸿蒙)系统中,关于自定义Dialog的宽度设置问题,可以通过以下方式进行控制:

在HarmonyOS的UI框架中,Dialog组件通常继承自Component类,其宽度和高度等布局属性可以通过XML布局文件或者代码动态设置。

  1. XML布局文件设置: 如果你是在XML文件中定义Dialog的布局,可以通过设置根布局的宽度属性来控制Dialog的宽度。例如,使用Width属性设置为MatchParent(匹配父容器宽度)、WrapContent(根据内容自动调整宽度)或者具体的数值(如300vp表示300垂直像素)。

  2. 代码动态设置: 在Java或Kotlin代码中,你可以通过获取Dialog的布局组件并设置其宽度。由于要求不使用Java相关内容,这里假设你使用的是鸿蒙的ArkUI(JS/TS)或其他支持的编程语言。在ArkUI中,你可以通过修改组件的样式对象来设置宽度。

    例如,在ArkUI中,你可以通过修改Dialog的style对象中的width属性来设置宽度,如this.dialog.style.width = '300vp';

请注意,具体的实现方式可能会因HarmonyOS的版本和使用的开发框架而有所差异。

如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html

回到顶部