windowClass.setWindowLayoutFullScreen(true)
this.topSafeHeight = this.getUIContext().px2vp(windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM).topRect.height)
windowClass.on('avoidAreaChange', (data) => {
if (data.type == window.AvoidAreaType.TYPE_SYSTEM) {
this.topSafeHeight = this.getUIContext().px2vp(data.area.topRect.height)
}
}) build() {
Stack({ alignContent: Alignment.TopStart }) {
// 顶部避让区域
Row() {
}
.padding({top:this.topSafeHeight})
.width("100%")
}
.height('100%')
} 应用代码中调用了setWindowLayoutFullScreen接口,打开了窗口沉浸式,故应用不会自动避让状态栏或者三点功能栏,而应用中未调用avoidAreaChange接口监听系统默认区域高度变化,动态配置顶部避让区域,导致悬浮窗场景下,页面内容未避让顶部窗口控制条。若代码中通过isLayoutFullScreen()判断来设置顶部margin值,由于悬浮窗模式下isLayoutFullScreen返回false,悬浮窗放大后isInFreeWindowMode也为false,无法通过全屏模式或悬浮窗模式判断来准确获取顶部避让高度。
应用开启沉浸式布局后,可以通过getWindowAvoidArea接口可获取屏幕顶部需要避让的矩阵区域topRect,获取到该值后应用可对应使用padding进行布局避让,并且注册on('avoidAreaChange')监听系统避让区域变化以进行布局的动态调整。不应通过isLayoutFullScreen()等全屏模式判断来设置顶部margin值,应统一使用安全区域高度进行避让。
import { window } from '@kit.ArkUI';
import { common } from '@kit.AbilityKit';
@Entry
@Component
struct topSafePage {
@State topSafeHeight: number = 0;
getWindow(): window.Window {
let context = this.getUIContext().getHostContext() as common.UIAbilityContext;
let windowStage = context.windowStage;
let windowClass = windowStage.getMainWindowSync();
return windowClass;
}
aboutToAppear(): void {
let windowClass: window.Window = this.getWindow();
this.topSafeHeight =
this.getUIContext().px2vp(windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM).topRect.height);
windowClass.setWindowLayoutFullScreen(true);
windowClass.on('avoidAreaChange', (data) => {
if (data.type === window.AvoidAreaType.TYPE_SYSTEM) {
this.topSafeHeight = this.getUIContext().px2vp(data.area.topRect.height);
}
});
}
build() {
Column() {
Row() {
// 内容区域
Text('进行顶部三点区域避让');
}
.width('100%')
.justifyContent(FlexAlign.Center);
}
// 使用padding进行避让
.padding({ top: this.topSafeHeight });
}
} // 在EntryAbility中添加安全区域高度监听
let avoidArea = mainWindow.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM);
let topHeight = px2vp(avoidArea.topRect.height);
AppStorage.setOrCreate('topSafeAreaHeight', topHeight);
mainWindow.on('avoidAreaChange', (data: window.AvoidAreaOptions) => {
if (data.type === window.AvoidAreaType.TYPE_SYSTEM) {
let newTopHeight = px2vp(data.area.topRect.height);
AppStorage.setOrCreate('topSafeAreaHeight', newTopHeight);
}
}); @StorageProp('topSafeAreaHeight') topSafeAreaHeight: number = 0;
// Header区域使用安全区域高度作为顶部margin
.margin({ top: this.topSafeAreaHeight })