HarmonyOS 自定义弹窗遮罩未全屏

HarmonyOS
2024-09-03 11:06:15
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zbw_apple

可以将文档中的示例一做如下改动,将  dialogController中的customStyle属性设置为 true,开启弹窗内容自定义。

struct CustomDialogUser { 
  ... 
  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: 0 }, 
    gridCount: 4, 
    customStyle: true, 
    cornerRadius: 10 
  }) 
  ... 
} 
 
//为了看出效果,将弹窗背景色改成red可以看出实现了完全遮挡。 
@CustomDialog 
struct CustomDialogExample { 
  build(){ 
  Column() { 
    ... 
  } 
  .backgroundColor(Color.Red) 
} 
}

尝试下如下代码:

@CustomDialog 
struct CustomDialogExample { 
  @Link textValue: string 
  @Link inputValue: string 
  controller?: CustomDialogController 
  // 若尝试在CustomDialog中传入多个其他的Controller,以实现在CustomDialog中打开另一个或另一些CustomDialog,那么此处需要将指向自己的controller放在所有controller的后面 
  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) 
    .backgroundColor(Color.Red) 
  } 
} 
 
@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: 0 }, 
    gridCount: 4, 
    customStyle: true, 
    cornerRadius: 10, 
    isModal: true 
  }) 
 
  // 在自定义组件即将析构销毁时将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%').height('100%').backgroundColor(Color.Yellow) 
  } 
}

想要实现全屏的展示,EntryAbility.ets的 onWindowStageCreate 添加如下代码,这样可以隐藏上下的导航栏部分。可以参考如下代码:

EntryAbility.ets

onWindowStageCreate(windowStage: window.WindowStage): void { 
  // Main window is created, set main page for this ability 
  hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); 
 
  // 1.获取应用主窗口。 
  let windowClass: window.Window | null = null; 
  windowStage.getMainWindow((err: BusinessError, data) => { 
  let errCode: number = err.code; 
  if (errCode) { 
    console.error('Failed to obtain the main window. Cause: ' + JSON.stringify(err)); 
    return; 
  } 
  windowClass = data; 
  console.info('Succeeded in obtaining the main window. Data: ' + JSON.stringify(data)); 
 
  // 2.实现沉浸式效果。方式一:设置导航栏、状态栏不显示。 
  let names: Array<'status' | 'navigation'> = []; 
  windowClass.setWindowSystemBarEnable(names, (err: BusinessError) => { 
  let errCode: number = err.code; 
  if (errCode) { 
    console.error('Failed to set the system bar to be visible. Cause:' + JSON.stringify(err)); 
    return; 
  } 
  console.info('Succeeded in setting the system bar to be visible.'); 
}); 
// 2.实现沉浸式效果。方式二:设置窗口为全屏布局,配合设置导航栏、状态栏的透明度、背景/文字颜色及高亮图标等属性,与主窗口显示保持协调一致。 
let isLayoutFullScreen = true; 
windowClass.setWindowLayoutFullScreen(isLayoutFullScreen, (err: BusinessError) => { 
  let errCode: number = err.code; 
  if (errCode) { 
    console.error('Failed to set the window layout to full-screen mode. Cause:' + JSON.stringify(err)); 
    return; 
  } 
  console.info('Succeeded in setting the window layout to full-screen mode.'); 
}); 
let sysBarProps: window.SystemBarProperties = { 
  statusBarColor: '#ff00ff', 
  navigationBarColor: '#00ff00', 
  // 以下两个属性从API Version 8开始支持 
  statusBarContentColor: '#ffffff', 
  navigationBarContentColor: '#ffffff' 
}; 
windowClass.setWindowSystemBarProperties(sysBarProps, (err: BusinessError) => { 
  let errCode: number = err.code; 
  if (errCode) { 
    console.error('Failed to set the system bar properties. Cause: ' + JSON.stringify(err)); 
    return; 
  } 
  console.info('Succeeded in setting the system bar properties.'); 
}); 
}) 
 
windowStage.loadContent('pages/Index', (err) => { 
  if (err.code) { 
    hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); 
    return; 
  } 
  hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); 
}); 
}
分享
微博
QQ
微信
回复
2024-09-03 17:40:54
相关问题
自定义弹窗自定义转场动画
654浏览 • 1回复 待解决
HarmonyOS 自定义弹窗CustomDialog问题
166浏览 • 1回复 待解决
HarmonyOS 自定义弹窗的问题
225浏览 • 1回复 待解决
使用自定义弹窗实现分享弹窗
178浏览 • 1回复 待解决
如何在自定义弹窗中再次弹窗
1877浏览 • 1回复 待解决
如何设置自定义弹窗位置
1728浏览 • 1回复 待解决
自定义弹窗如何嵌套使用
843浏览 • 1回复 待解决
自定义弹窗使用相关问题
630浏览 • 1回复 待解决
如何自定义popup弹窗的布局?
193浏览 • 2回复 待解决
自定义弹窗大小如何自适应内容
1984浏览 • 1回复 待解决
如何去除自定义弹窗的白色背景
1847浏览 • 1回复 待解决
弹窗打开、关闭动画是否支持自定义
1854浏览 • 1回复 待解决
开发自定义弹窗时报错如下
190浏览 • 1回复 待解决
如何理解自定义弹窗中的gridCount参数
1837浏览 • 1回复 待解决