HarmonyOS 鸿蒙Next 获取屏幕宽度和导航栏高度为0
HarmonyOS 鸿蒙Next 获取屏幕宽度和导航栏高度为0
// Main window is created, set main page for this ability
hilog.info(0x0000, ‘testTag’, ‘%{public}s’, ‘Ability onWindowStageCreate’);
// this.setFullSize(windowStage);
windowStage.loadContent(‘pages/PianoPage’, this.localStorage, (err) => {
if (err.code) {
hilog.error(0x0000, ‘testTag’, ‘Failed to load the content. Cause: %{public}s’, JSON.stringify(err) ?? ‘’);
return;
}
let windowClass: window.Window = windowStage.getMainWindowSync(); // 获取应用主窗口
CommonConstants.WINDOW_WIDTH = windowClass.getWindowProperties().windowRect.width / display.getDefaultDisplaySync().densityPixels;
let area = windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR);
CommonConstants.NAV_HEIGHT = area.bottomRect.height / display.getDefaultDisplaySync().densityPixels;
// 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));
});
// 2. 设置状态栏和导航条隐藏
windowClass.setSpecificSystemBarEnabled(‘status’, false)
.then(() => {
console.info(‘Succeeded in setting the status bar to be invisible.’);
})
.catch((err: BusinessError) => {
console.error(
Failed to set the status bar to be invisible. Code is ${err.code}, message is ${err.message}
);});
});
}
如上函数,跳转到一个UIAbility,在他的onWindowStageCreate函数下执行上述操作,得到的 CommonConstants.WINDOW_WIDTH 和CommonConstants.NAV_HEIGHT有概率为0,特别是折叠屏全部展开的状态下。
如何才能准确获取屏幕高宽值,和导航栏高度
更多关于HarmonyOS 鸿蒙Next 获取屏幕宽度和导航栏高度为0的实战教程也可以访问 https://www.itying.com/category-93-b0.html
display.getAllDisplays((err, data) => {
let screenWidth : number = data[0].width
let screenHeight : number = data[0].height
console.log('width = ' + screenWidth + 'height = ' + screenHeight)
console.log('width + height = ' + JSON.stringify(data))
})
导航栏:
let windowClass: window.Window = windowStage.getMainWindowSync(); // 获取应用主窗口
let type = window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR; // 以导航条避让为例
let avoidArea = windowClass.getWindowAvoidArea(type);
let bottomRectHeight = avoidArea.bottomRect.height; // 获取到导航条区域的高度
console.log("bottomRectHeight: " + bottomRectHeight)
let area = windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM);
let statusBarHeight = px2vp(area.topRect.height) //状态栏高度
console.log("statusBarHeight: " + statusBarHeight)
更多关于HarmonyOS 鸿蒙Next 获取屏幕宽度和导航栏高度为0的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS鸿蒙Next系统中,如果你遇到获取屏幕宽度和导航栏高度为0的问题,这通常是由于权限未正确配置或代码实现有误导致的。以下是一些直接相关的解决步骤:
-
权限配置: 确保你的应用已在
config.json
中配置了必要的权限,包括访问窗口和显示信息的权限。检查是否包含了ohos.permission.READ_SETTINGS
或相关显示权限。 -
代码实现: 使用正确的API来获取屏幕宽度和导航栏高度。在HarmonyOS中,你可以通过
WindowManager
或Display
相关API来获取这些信息。例如,使用WindowManager.getDefault().getDisplay().getSize(point)
来获取屏幕宽高,其中point.x
为宽度,point.y
为高度。 -
检查API版本: 确保你使用的API与你的鸿蒙系统版本兼容。不同版本的鸿蒙系统可能提供了不同的API或修改了现有API的行为。
-
调试与日志: 使用调试工具和日志输出来检查相关变量的值,确认在代码执行过程中,屏幕宽度和导航栏高度的值是否被正确设置。
如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html 。