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) 
} 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.

尝试下如下代码:

@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) 
  } 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.

想要实现全屏的展示,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.'); 
}); 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
分享
微博
QQ
微信
回复
2024-09-03 17:40:54
相关问题
HarmonyOS 自定义全屏dialog
889浏览 • 1回复 待解决
自定义弹窗自定义转场动画
1930浏览 • 1回复 待解决
HarmonyOS 自定义弹框遮罩透传问题
703浏览 • 1回复 待解决
HarmonyOS 自定义弹窗选择
1298浏览 • 1回复 待解决
自定义弹框,遮罩背景颜色无法设置
995浏览 • 1回复 待解决
HarmonyOS 自定义弹窗CustomDialog
748浏览 • 1回复 待解决
HarmonyOS 自定义弹框不能全屏
888浏览 • 1回复 待解决
使用自定义弹窗实现分享弹窗
1343浏览 • 1回复 待解决
HarmonyOS 自定义弹窗的问题
1601浏览 • 1回复 待解决
HarmonyOS 自定义弹窗CustomDialog问题
1346浏览 • 1回复 待解决
HarmonyOS 自定义弹窗层级问题
952浏览 • 1回复 待解决
HarmonyOS 全局自定义弹窗demo
1187浏览 • 1回复 待解决
HarmonyOS 自定义弹窗控制问题
968浏览 • 1回复 待解决
HarmonyOS 自定义弹窗关闭问题
907浏览 • 1回复 待解决
HarmonyOS 使用全局自定义弹窗
768浏览 • 1回复 待解决
HarmonyOS 自定义弹窗CustomDialog 问题
775浏览 • 1回复 待解决