应用开发中,窗口默认是非沉浸式的,这意味着页面不能扩展到顶部状态栏和底部导航栏避让区,这会导致状态栏和导航栏避让区与页面颜色不一致的问题,因此应用若为了提升沉浸体验效果,需要手动设置沉浸式。
应用通过设置Window.setWindowLayoutFullScreen()沉浸式布局,以及Window.setWindowSystemBarEnable()状态栏的显隐,可以控制应用窗口沉浸式全屏。示例如下:
import { BusinessError } from '@kit.BasicServicesKit';
import { window } from '@kit.ArkUI';
@Entry
@Component
struct Index {
isFullScreen: boolean = false;
private context = this.getUIContext().getHostContext();
windowClass: window.Window | undefined = undefined;
@State ButtonMsg: string = '沉浸式全屏';
aboutToAppear(): void {
window.getLastWindow(this.context, (err: BusinessError, window) => {
this.windowClass = window;
console.error(`failed to getLastWindow successfully errCode is: ${err.code}, ${err.message}`);
});
}
// 进入全屏
enterFullscreen() {
if (this.windowClass != undefined) {
this.windowClass.setWindowLayoutFullScreen(!this.isFullScreen);
this.windowClass.setWindowSystemBarEnable([]);
this.isFullScreen = !this.isFullScreen;
}
}
// 退出全屏
exitFullscreen() {
if (this.windowClass != undefined) {
this.windowClass.setWindowLayoutFullScreen(!this.isFullScreen);
this.windowClass.setWindowSystemBarEnable(['status', 'navigation']);
this.isFullScreen = !this.isFullScreen;
}
}
build() {
Column() {
Stack({ alignContent: Alignment.Center }) {
Text('Window show')
.backgroundColor(Color.Black)
.fontColor(Color.White)
.textAlign(TextAlign.Center)
.height('100%')
.width('100%');
Button(this.ButtonMsg)
.onClick(() => {
if (this.isFullScreen) {
this.exitFullscreen();
this.ButtonMsg = '进入沉浸式';
} else {
this.enterFullscreen();
this.ButtonMsg = '退出沉浸式';
}
});
}
.height('100%')
.width('100%');
}
.width('100%')
.height('100%');
}
}