HarmonyOS 如何设置背景透明度?

希望提供可设置背景透明度的相关接口。

阅读 575
1 个回答

目前toast不支持自定义样式,可以考虑使用promptAction.openCustomDialog。(支持与页面解耦,支持自定义圆角、字体大小、颜色、背景等) demo如下:

import { BusinessError } from '@ohos.base';
import { ComponentContent } from "@ohos.arkui.node";

class Params {
  text: string = ""

  constructor(text: string) {
    this.text = text;
  }
}

@Builder
function buildText(params: Params) {
  Column() {
    Text(params.text).fontSize(50).fontWeight(FontWeight.Bold).margin({ bottom: 36 })
  }.backgroundColor('#FFF0F0F0').borderRadius(25)
}

@Entry
@Component
struct CustomDialog {
  @State message: string = "弹窗弹窗"

  build() {
    Row() {
      Column() {
        Button("click me").onClick(() => {
          let uiContext = this.getUIContext();
          let promptAction = uiContext.getPromptAction();
          let contentNode = new ComponentContent(uiContext, wrapBuilder(buildText), new Params(this.message));
          try {
            promptAction.openCustomDialog(contentNode);
          } catch (error) {
            let message = (error as BusinessError).message;
            let code = (error as BusinessError).code;
            console.error(`OpenCustomDialog args error code is ${code}, message is ${message}`);
          };
        })
      }.width('100%').height('100%')
    }.height('100%')
  }
}

参考:https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis-arkui/js-apis-arkui-UIContext.md\#opencustomdialog12

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进