@ohos.window (窗口)提供了窗口管理器WindowStage,用于管理各个基本窗口单元。同时,可通过getWindowProperties方法获取当前窗口的属性,其中isLayoutFullScreen可以判断窗口是否为沉浸式且处于全屏模式。
初次创建SDK项目可参考创建及发布三方库。利用DevEco Studio对开发后的库模块打成HAR包,详情请见:构建HAR。在项目中引入三方库参考配置依赖项。
import { window } from '@kit.ArkUI';
export function saveWindowStage(windowStage: window.WindowStage) {
AppStorage.setAndLink('windowStage', windowStage);
}
export function getFullScreenStatus(): string {
let winStage: window.WindowStage = AppStorage.get('windowStage') as window.WindowStage;
let isLayoutFullScreen = winStage.getMainWindowSync().getWindowProperties().isLayoutFullScreen;
console.info(isLayoutFullScreen ? 'isFullScreen' : 'isNotFullScreen');
// 返回结果给应用页,如不需要则不返回。在应用侧直接调用该函数。
return (isLayoutFullScreen ? 'isFullScreen' : 'isNotFullScreen');
} import { PromptAction } from '@kit.ArkUI';
import { getFullScreenStatus } from 'sdkname';
import { common } from '@kit.AbilityKit';
@Entry
@Component
struct Index {
@State isFullScreen: boolean = false; // 切换屏幕全屏
@State state: string = ''; // 存储HAR中获取沉浸式状态
promptAction: PromptAction = this.getUIContext().getPromptAction();
context = this.getUIContext().getHostContext() as common.UIAbilityContext;
aboutToAppear(): void {
// 通过AppStorage.setAndLink存储WindowStage
AppStorage.setAndLink('windowStage', this.context.windowStage);
// 进入应用调用HAR方法获取屏幕状态
this.state = getFullScreenStatus();
this.promptAction.showToast({
message: this.state,
duration: 2000
});
}
build() {
Row() {
Column({ space: 5 }) {
Text('获取宿主应用窗口是否沉浸式')
.fontSize(30)
.fontWeight(FontWeight.Bold);
Button('changeFullScreenStatus').onClick(async () => {
// 切换屏幕状态
this.isFullScreen = !this.isFullScreen;
await this.context.windowStage.getMainWindowSync().setWindowLayoutFullScreen(this.isFullScreen);
// 调用SDK方法获取屏幕状态
this.state = getFullScreenStatus();
this.promptAction.showToast({
message: this.state,
duration: 2000
});
});
}
.width('100%');
}
.height('100%')
.backgroundColor('#eaeaea');
}
}