通过mainWindow.setWindowLayoutFullScreen(true)进入沉浸式模式后,页面如何避开状态栏(HarmonyOS 鸿蒙Next)
通过mainWindow.setWindowLayoutFullScreen(true)进入沉浸式模式后,页面如何避开状态栏(HarmonyOS 鸿蒙Next) 通过给最外层组件添加padding?
3 回复
官方文档有个沉浸式方案 里面有些怎么处理
更多关于通过mainWindow.setWindowLayoutFullScreen(true)进入沉浸式模式后,页面如何避开状态栏(HarmonyOS 鸿蒙Next)的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
先获取状态栏和导航栏,再加padding,
在鸿蒙Next中,通过mainWindow.setWindowLayoutFullScreen(true)
进入沉浸式模式后,页面默认会覆盖状态栏。若需要避开状态栏,可以通过Window
的setWindowLayout
方法,结合WindowManager
获取状态栏高度,调整页面布局。具体步骤如下:
- 使用
WindowManager.getSystemWindowInsets()
获取状态栏高度。 - 通过
Window.setWindowLayout()
设置页面布局时,将状态栏高度考虑在内,确保页面内容不被状态栏遮挡。
示例代码:
import window from '@ohos.window';
let windowClass = window.getTopWindow();
windowClass.then((win) => {
win.setWindowLayoutFullScreen(true);
let insets = win.getWindowSystemInsets();
let statusBarHeight = insets.top;
win.setWindowLayout({
x: 0,
y: statusBarHeight,
width: '100%',
height: '100%-' + statusBarHeight
});
});
此方法通过获取状态栏高度并调整页面布局,确保页面内容避开状态栏。