方案一:使用"window_pcmode_switch_status"字段结合settings.registerKeyObserver()接口和settings.getValue()接口来获取并监听当前是否切换至自由多窗模式。当应用开启自由多窗模式时,将根组件设置的padding属性值清空,防止因为开启沉浸式模式界面底部导航栏和页面状态栏有留白。当应用关闭自由多窗模式时,开启沉浸式模式并且根组件动态设置padding属性值用于动态设置状态栏和底部导航栏避让区域,完整示例参考如下:import { settings } from '@kit.BasicServicesKit';
import { window } from '@kit.ArkUI';
@Entry
@Component
struct TabsExample {
private controller: TabsController = new TabsController();
@StorageProp('bottomRectHeight')
bottomRectHeight: number = 0;
@StorageProp('topRectHeight')
topRectHeight: number = 0;
private context = this.getUIContext().getHostContext();
aboutToAppear(): void {
//初次获取当前的状态
this.getMode();
// 监听当前是否切换至自由多窗模式
settings.registerKeyObserver(this.context, 'window_pcmode_switch_status', settings.domainName.USER_PROPERTY,
() => {
this.getMode();
});
}
getMode() {
settings.getValue(this.context, 'window_pcmode_switch_status', settings.domainName.USER_PROPERTY)
.then((data) => {
if (data) {
this.setFullScreen(false);
this.topRectHeight = 0;
this.bottomRectHeight = 0;
} else {
this.setFullScreen(true);
this.setSafeTop();
this.setSafeBottom();
}
});
}
// 动态设置窗口全屏
setFullScreen(value: boolean) {
window.getLastWindow(this.context).then(currentWindow => {
currentWindow.setWindowLayoutFullScreen(value);
});
};
// 设置顶部状态栏避让
setSafeTop() {
window.getLastWindow(this.context).then(windowClass => {
let topType = window.AvoidAreaType.TYPE_SYSTEM;
let statusBarArea = windowClass.getWindowAvoidArea(topType);
this.topRectHeight = statusBarArea.topRect.height;
});
}
// 设置底部导航栏避让
setSafeBottom() {
window.getLastWindow(this.context).then(windowClass => {
let bottomType = window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR;
let bottomArea = windowClass.getWindowAvoidArea(bottomType);
this.bottomRectHeight = bottomArea.bottomRect.height;
});
}
build() {
Column() {
Tabs({ barPosition: BarPosition.End, controller: this.controller }) {
TabContent() {
Column() {
}.width('100%').height('100%').backgroundColor('#F1F3F5');
}.tabBar(BottomTabBarStyle.of($r('sys.media.AI_search'), 'Tab1'));
TabContent() {
Column().width('100%').height('100%').backgroundColor('#F1F3F5');
}.tabBar(BottomTabBarStyle.of($r('sys.media.AI_phone'), 'Tab2'));
TabContent() {
Column().width('100%').height('100%').backgroundColor('#F1F3F5');
}.tabBar(BottomTabBarStyle.of($r('sys.media.AI_play'), 'Tab3'));
TabContent() {
Column().width('100%').height('100%').backgroundColor('#F1F3F5');
}.tabBar(BottomTabBarStyle.of($r('sys.media.AI_playing'), 'Tab4'));
}
.width('100%')
.height('100%')
.backgroundColor('#F1F3F5')
// top数值与状态栏区域高度保持一致;bottom数值与导航区域高度保持一致
.padding({
top: this.getUIContext().px2vp(this.topRectHeight),
bottom: this.getUIContext().px2vp(this.bottomRectHeight)
});
}
.height('100%')
.width('100%');
}
} 方案二:通过isInFreeWindowMode和on('freeWindowModeChange')来获取并监听当前是否切换至自由多窗模式,当应用开启自由多窗模式时,将根组件设置的padding属性值清空,防止因为开启沉浸式模式界面底部导航栏和页面状态栏有留白。当应用关闭自由多窗模式时,开启沉浸式模式并且根组件动态设置padding属性值用于动态设置状态栏和底部导航栏避让区域,完整示例参考如下:import { window } from '@kit.ArkUI';
@Entry
@Component
struct Page {
private controller: TabsController = new TabsController();
@StorageProp('bottomRectHeight')
bottomRectHeight: number = 0;
@StorageProp('topRectHeight')
topRectHeight: number = 0;
private context = this.getUIContext().getHostContext();
aboutToAppear(): void {
// 监听当前是否切换至自由多窗模式
window.getLastWindow(this.context, (err: BusinessError, data) => {
const errCode: number = err.code;
if (errCode) {
console.error(`Failed to obtain the top window. Cause code: ${err.code}, message: ${err.message}`);
return;
}
let isInFreeWindowMode: boolean = data.isInFreeWindowMode();
this.configUIWithFreeWindowMode(isInFreeWindowMode);
data.on('freeWindowModeChange', callback);
});
const callback = (isInFreeWindowMode: boolean) => {
this.configUIWithFreeWindowMode(isInFreeWindowMode);
};
}
configUIWithFreeWindowMode(isFreeWindowMode: boolean) {
if (isFreeWindowMode) {
this.setFullScreen(false);
this.topRectHeight = 0;
this.bottomRectHeight = 0;
} else {
this.setFullScreen(true);
this.setSafeTop();
this.setSafeBottom();
}
}
// 动态设置窗口全屏
setFullScreen(value: boolean) {
window.getLastWindow(this.context).then(currentWindow => {
currentWindow.setWindowLayoutFullScreen(value);
});
};
// 设置顶部状态栏避让
setSafeTop() {
window.getLastWindow(this.context).then(windowClass => {
let topType = window.AvoidAreaType.TYPE_SYSTEM;
let statusBarArea = windowClass.getWindowAvoidArea(topType);
this.topRectHeight = statusBarArea.topRect.height;
});
}
// 设置底部导航栏避让
setSafeBottom() {
window.getLastWindow(this.context).then(windowClass => {
let bottomType = window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR;
let bottomArea = windowClass.getWindowAvoidArea(bottomType);
this.bottomRectHeight = bottomArea.bottomRect.height;
});
}
build() {
Column() {
Tabs({ barPosition: BarPosition.End, controller: this.controller }) {
TabContent() {
Column() {
}.width('100%').height('100%').backgroundColor('#F1F3F5');
}.tabBar(BottomTabBarStyle.of($r('sys.media.AI_search'), 'Tab1'));
TabContent() {
Column().width('100%').height('100%').backgroundColor('#F1F3F5');
}.tabBar(BottomTabBarStyle.of($r('sys.media.AI_phone'), 'Tab2'));
TabContent() {
Column().width('100%').height('100%').backgroundColor('#F1F3F5');
}.tabBar(BottomTabBarStyle.of($r('sys.media.AI_play'), 'Tab3'));
TabContent() {
Column().width('100%').height('100%').backgroundColor('#F1F3F5');
}.tabBar(BottomTabBarStyle.of($r('sys.media.AI_playing'), 'Tab4'));
}
.width('100%')
.height('100%')
.backgroundColor('#F1F3F5')
// top数值与状态栏区域高度保持一致;bottom数值与导航区域高度保持一致
.padding({
top: this.getUIContext().px2vp(this.topRectHeight),
bottom: this.getUIContext().px2vp(this.bottomRectHeight)
});
}
.height('100%')
.width('100%');
}
}