鸿蒙开发中如何获取屏幕宽高?
使用 window获取:
window.getLastWindow(getContext(this))
.then((windowClass: window.Window) => {
let type = window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR; // 以导航条避让为例
let avoidArea = windowClass.getWindowAvoidArea(type);
this.bottomSafeHeight = px2vp(avoidArea.bottomRect.height); // 获取到导航条区域的高度
type = window.AvoidAreaType.TYPE_SYSTEM;
avoidArea = windowClass.getWindowAvoidArea(type);
this.statusBarHeight = px2vp(avoidArea.topRect.height);
this.width = px2vp(windowClass.getWindowProperties().windowRect.width);
this.height = px2vp(windowClass.getWindowProperties().windowRect.height);
this.layoutWidth = this.width;
this.layoutHeight = this.height - this.statusBarHeight - this.bottomSafeHeight;
})
.catch((error: Error) => {
})
完整代码参见:
HM4Demo/base/src/main/ets/utils/ScreenUtil.ets at master · Dev-Wiki/HM4Demo