HarmonyOS 鸿蒙Next实现页面全屏功能示例代码

HarmonyOS 鸿蒙Next实现页面全屏功能示例代码

介绍

本示例通过windowClass.setWindowLayoutFullScreen设置页面是否全屏。

实现页面全屏功能源码链接

效果预览

图片名称

使用说明

  1. 点击“全屏“按钮,设置页面为全屏
  2. 点击“关闭全屏按钮”,关闭页面全屏

实现思路

  1. 设置窗口全屏
let windowClass = AppStorage.get("mainWindow") as window.Window
// 1. 设置窗口全屏
windowClass.setWindowLayoutFullScreen(true)
  .then(() => {
    hilog.info(0x0000,'test','Succeeded in setting the window layout to full-screen mode.');
  })
  .catch((err: BusinessError) => {
    hilog.error(0x0000,'test','Failed to set the window layout to full-screen mode. Cause:' + JSON.stringify(err));
  });
  1. 关闭窗口全屏
let windowClass = AppStorage.get("mainWindow") as window.Window
// 1. 关闭窗口全屏
windowClass.setWindowLayoutFullScreen(false)
  .then(() => {
    hilog.info(0x0000,'test','Succeeded in setting the window layout to full-screen mode.');
  })
  .catch((err: BusinessError) => {
    hilog.error(0x0000,'test','Failed to set the window layout to full-screen mode. Cause:' + JSON.stringify(err));
  });

更多关于HarmonyOS 鸿蒙Next实现页面全屏功能示例代码的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

2in1 不支持吗?

更多关于HarmonyOS 鸿蒙Next实现页面全屏功能示例代码的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


开发者您好,2in1 目前是不支持的。

在HarmonyOS(鸿蒙Next)中,实现页面全屏功能可以通过调用Window模块的setFullScreen方法来实现。以下是一个简单的示例代码,展示了如何将页面设置为全屏模式:

import window from '@ohos.window';

async function setFullScreen() {
    try {
        // 获取当前窗口
        let windowClass = await window.getLastWindow(this.context);
        // 设置窗口为全屏模式
        await windowClass.setFullScreen(true);
        console.info('Full screen mode set successfully.');
    } catch (error) {
        console.error(`Failed to set full screen mode, error code: ${error.code}, message: ${error.message}`);
    }
}

在这个示例中,window.getLastWindow用于获取当前窗口对象,windowClass.setFullScreen(true)则将窗口设置为全屏模式。如果设置成功,控制台会输出成功信息;如果失败,则会捕获并输出错误信息。

请注意,这段代码需要在HarmonyOS的开发环境中运行,并且需要确保你的应用具有相应的权限来修改窗口状态。

回到顶部