Openharmony开发记录-全屏

解决方法

在stage模型中要开启全屏一共有两种方法

方法一:

在onWindowStageCreate函数中,通过windowStage.getMainWindow()获取到窗口对象

然后调用

data.setFullScreen(true, (err, data) => {
        if (err.code) {
          console.error('Failed to enable the full-screen mode. Cause:' + JSON.stringify(err));
          return;
        }
        console.info('Succeeded in enabling the full-screen mode. Data: ' + JSON.stringify(data));
      });

完整代码

 onWindowStageCreate(windowStage: window.WindowStage) {
    // Main window is created, set main page for this ability
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');

    windowStage.loadContent('pages/Index', (err, data) => {
      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. Data: %{public}s', JSON.stringify(data) ?? '');
    });

    // 设置全屏
    windowStage.getMainWindow().then((data) => {
      data.setFullScreen(true, (err, data) => {
        if (err.code) {
          console.error('Failed to enable the full-screen mode. Cause:' + JSON.stringify(err));
          return;
        }
        console.info('Succeeded in enabling the full-screen mode. Data: ' + JSON.stringify(data));
      });
    })


  }

方法二

在子页面中同样也是获取到window对象,然后调用window.getLastWindow(this.context),即可获取到windowclass,调用windowclass中的setWindowSystemBarEnable([])即可设置全屏

完整代码

// 获取UIAbility上下文
  context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext

  async setSystemBar() {
    let windowClass = await window.getLastWindow(this.context)
    //设置导航栏,状态栏不可见
    await windowClass.setWindowSystemBarEnable([])
  }

  aboutToAppear() {
    this.setSystemBar()
  }

最后附上API10的所有窗口函数

实例名接口名描述
WindowStagegetMainWindow(callback: AsyncCallback<Window>): void获取WindowStage实例下的主窗口。
此接口仅可在Stage模型下使用。
WindowStageloadContent(path: string, callback: AsyncCallback<void>): void为当前WindowStage的主窗口加载具体页面。
此接口仅可在Stage模型下使用。
WindowStagecreateSubWindow(name: string, callback: AsyncCallback<Window>): void创建子窗口。
此接口仅可在Stage模型下使用。
window静态方法createWindow(config: Configuration, callback: AsyncCallback<Window>): void创建系统窗口。
-config:创建窗口时的参数。
WindowsetUIContent(path: string, callback: AsyncCallback<void>): void为当前窗口加载具体页面。
WindowsetWindowBackgroundColor(color: string, callback: AsyncCallback<void>): void设置窗口的背景色。
WindowsetWindowBrightness(brightness: number, callback: AsyncCallback<void>): void设置屏幕亮度值。
WindowsetWindowTouchable(isTouchable: boolean, callback: AsyncCallback<void>): void设置窗口是否为可触状态。
WindowmoveWindowTo(x: number, y: number, callback: AsyncCallback<void>): void移动当前窗口位置。
Windowresize(width: number, height: number, callback: AsyncCallback<void>): void改变当前窗口大小。
WindowsetWindowLayoutFullScreen(isLayoutFullScreen: boolean, callback: AsyncCallback<void>): void设置窗口布局是否为全屏布局。
WindowsetWindowSystemBarEnable(names: Array<‘status’|‘navigation’>): Promise<void>设置导航栏、状态栏是否显示。
WindowsetWindowSystemBarProperties(systemBarProperties: SystemBarProperties, callback: AsyncCallback<void>): void设置窗口内导航栏、状态栏属性。
systemBarProperties:导航栏、状态栏的属性集合。
WindowshowWindow(callback: AsyncCallback<void>): void显示当前窗口。
Windowon(type: ‘touchOutside’, callback: Callback<void>): void开启本窗口区域外的点击事件的监听。
WindowdestroyWindow(callback: AsyncCallback<void>): void销毁当前窗口。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值