HarmonyOS鸿蒙Next中开启沉浸式显示并存储安全区高度
HarmonyOS鸿蒙Next中开启沉浸式显示并存储安全区高度
可以写在entryAbility里面或者在index页面的aboutToAppear里面,开启沉浸式阅读模式之后将安全区的高度存入首选项中,在不同需求的页面进行设置
```javascript
window.getLastWindow(getContext()).then(win => {
win.setWindowLayoutFullScreen(true)
let topAvoid = win.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM)
let bottomAvoid = win.getWindowAvoidArea(window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR)
const topHeight = px2vp(topAvoid.topRect.height)
const bottomHeight = px2vp(bottomAvoid.bottomRect.height)
const avoidHeight: number[] = [topHeight, bottomHeight]
AppStorage.setOrCreate('avoidHeight', avoidHeight)
})
更多关于HarmonyOS鸿蒙Next中开启沉浸式显示并存储安全区高度的实战教程也可以访问 https://www.itying.com/category-93-b0.html
2 回复
在HarmonyOS鸿蒙Next中,开启沉浸式显示并存储安全区高度可以通过以下步骤实现:
- 开启沉浸式显示:在
MainAbility的onStart方法中,使用Window的setLayoutFullScreen方法将布局设置为全屏模式,并通过setStatusBarVisibility和setNavigationBarVisibility方法隐藏状态栏和导航栏。
onStart() {
let windowClass = this.context.getWindow();
windowClass.setLayoutFullScreen(true);
windowClass.setStatusBarVisibility(false);
windowClass.setNavigationBarVisibility(false);
}
- 获取安全区高度:通过
Window的getSystemAvoidArea方法获取系统避让区域的高度,并将其存储在全局变量或本地存储中。
onStart() {
let windowClass = this.context.getWindow();
let avoidArea = windowClass.getSystemAvoidArea(window.AvoidAreaType.TYPE_SYSTEM);
let safeAreaHeight = avoidArea.topRect.height; // 顶部安全区高度
// 将safeAreaHeight存储在全局变量或本地存储中
}
更多关于HarmonyOS鸿蒙Next中开启沉浸式显示并存储安全区高度的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS鸿蒙Next中,可以通过WindowManager获取安全区高度并开启沉浸式显示。首先,使用getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)实现全屏显示。然后,通过WindowInsets获取安全区高度,如getWindow().getDecorView().getRootWindowInsets().getSystemWindowInsets(),存储该值以便后续使用。最后,调整UI布局以适应安全区,确保内容不被遮挡。

