HarmonyOS NEXT 适配安全区

1.  首先在   EntryAbility   文件中引入 

import { window } from '@kit.ArkUI';
import { BusinessError } from '@kit.BasicServicesKit';

2、在  onWindowStageCreate  方法中  使页面全屏  

let windowClass: window.Window = windowStage.getMainWindowSync(); // 获取应用主窗口
// 1. 设置窗口全屏
let isLayoutFullScreen = true;
windowClass.setWindowLayoutFullScreen(isLayoutFullScreen).then(() => {
  console.info('Succeeded in setting the window layout to full-screen mode.');
}).catch((err: BusinessError) => {
  console.error('Failed to set the window layout to full-screen mode. Cause:' + JSON.stringify(err));
});

3、创建两个参数保存起来 (还是在  onWindowStageCreate  中)

let typeBottom = window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR; // 以导航条避让为例
let avoidArea = windowClass.getWindowAvoidArea(typeBottom);
let bottomRectHeight = avoidArea.bottomRect.height; // 获取到导航条区域的高度
AppStorage.setOrCreate('bottomRectHeight', bottomRectHeight);

let typeTop = window.AvoidAreaType.TYPE_SYSTEM; // 以状态栏避让为例
avoidArea = windowClass.getWindowAvoidArea(typeTop);
let topRectHeight = avoidArea.topRect.height; // 获取状态栏区域高度
AppStorage.setOrCreate('topRectHeight', topRectHeight);

4、监听 方法  并存储获取到的顶部安全区高度和底部安全区高度 (还是在  onWindowStageCreate  中)

// 监听
windowClass.on('avoidAreaChange', (data) => {
  if (data.type === window.AvoidAreaType.TYPE_SYSTEM) {
    let topRectHeight = data.area.topRect.height;
    AppStorage.setOrCreate('topRectHeight', topRectHeight);
  } else if (data.type == window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR) {
    let bottomRectHeight = data.area.bottomRect.height;
    AppStorage.setOrCreate('bottomRectHeight', bottomRectHeight);
  }
});

5、 在页面中声明变量 接收就行了

// 底部避让区域高度
@StorageProp('bottomRectHeight')
bottomRectHeight: number = 0;
// 顶部高度
@StorageProp('topRectHeight')
topRectHeight: number = 0;

6、使用padding 挤出来安全区的高度  即可

.padding({
  bottom: px2vp(this.bottomRectHeight)
})
.padding({
  top: px2vp(this.topRectHeight)
})

7、效果展示  

原页面

改后页面

希望对你有所帮助

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值