HarmonyOS鸿蒙Next中CustomDialogController弹出时底部安全区颜色为白色,怎么设置能够去掉或者覆盖其他颜色

HarmonyOS鸿蒙Next中CustomDialogController弹出时底部安全区颜色为白色,怎么设置能够去掉或者覆盖其他颜色 CustomDialogController 弹出的时候底部安全区颜色为白色,怎么设置能够去掉或者覆盖其他颜色

3 回复

您可以设置isModal为false,参考链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-methods-custom-dialog-box-V5#customdialogcontrolleroptions%E5%AF%B9%E8%B1%A1%E8%AF%B4%E6%98%8E

也可以参考如下代码:

@CustomDialog

struct CustomDialogExample {

  @Link textValue: string

  @Link inputValue: string

  dialogControllerTwo: CustomDialogController | null = new CustomDialogController({
    builder: CustomDialogExample,
    customStyle:true
  })

  controller?: CustomDialogController

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

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

  build() {
    Column() {
      TextInput({ placeholder: '', text: this.textValue }).height(60).width('90%')
        .onChange((value: string) => {
          this.textValue = value
        })
      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)
  }
}

@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,
    }),
    autoCancel: true,
    alignment: DialogAlignment.Bottom,
    offset: { dx: 0, dy: -180 },
    customStyle: false,
    cornerRadius: 10,
    backgroundColor:'#ffaa50e9',
    isModal:false
  })

  aboutToDisappear() {
    this.dialogController = null
  }

  onCancel() {
  }

  onAccept() {
  }

  build() {
    Column() {
      Button(this.inputValue)
        .onClick(() => {
          if (this.dialogController != null) {
            this.dialogController.open()
          }
        }).backgroundColor(0x317aff)
    }.width('100%').margin({ top: 5 })
  }
}

更多关于HarmonyOS鸿蒙Next中CustomDialogController弹出时底部安全区颜色为白色,怎么设置能够去掉或者覆盖其他颜色的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,CustomDialogController弹出时底部安全区颜色默认为白色。要修改或覆盖该颜色,可以通过自定义对话框的布局文件或直接在代码中设置背景颜色。具体步骤如下:

  1. 自定义布局文件:在resources/base/layout目录下创建或修改对话框的布局文件。在根布局中设置android:background属性为所需颜色或使用ohos:background_element来指定背景元素。
<DirectionalLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:width="match_parent"
    ohos:height="match_parent"
    ohos:background_element="#FF0000"> <!-- 设置背景颜色为红色 -->
    <!-- 其他布局内容 -->
</DirectionalLayout>
  1. 代码中设置背景:在CustomDialogControlleronCreateComponent方法中,通过Component.setBackground方法动态设置背景颜色。
@Override
protected Component onCreateComponent(ComponentContainer componentContainer) {
    DirectionalLayout layout = (DirectionalLayout) LayoutScatter.getInstance(getContext())
        .parse(ResourceTable.Layout_custom_dialog_layout, null, false);
    layout.setBackground(new ShapeElement().setRgbColor(new RgbColor(255, 0, 0))); // 设置背景颜色为红色
    return layout;
}
  1. 安全区处理:如果需要对底部安全区进行特殊处理,可以使用WindowManager相关API获取安全区信息,并在布局中预留相应空间或调整布局。
WindowManager windowManager = getWindow().getWindowManager();
Rect safeArea = windowManager.getSafeArea();

通过以上方法,可以自定义或覆盖CustomDialogController弹出时底部安全区的颜色。

在HarmonyOS鸿蒙Next中,CustomDialogController弹出时底部安全区颜色默认白色,可以通过以下方法调整或覆盖:

  1. 设置背景颜色:在CustomDialog的布局文件中,为根布局设置背景颜色,覆盖默认白色。

    android:background="@color/your_color"
    
  2. 适配安全区:使用WindowInsets适配安全区,确保布局内容不被遮挡,同时调整安全区颜色。

    getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
    
  3. 自定义Dialog样式:在styles.xml中定义自定义Dialog样式,设置窗口背景颜色。

    <style name="CustomDialogTheme" parent="Theme.AppCompat.Dialog">
        <item name="android:windowBackground">@color/your_color</item>
    </style>
    

通过这些方法,可以自定义或覆盖底部安全区的颜色。

回到顶部