鸿蒙5.0开发常见问题【如何获取屏幕顶部状态栏、底部导航栏和导航条的高度?】
摘要:文章介绍了如何使用ArkUI框架的window.getWindowAvoidArea方法获取系统避让区域高度。示例代码展示了获取系统默认区域(包括状态栏和导航栏)以及导航指示器区域的高度,包括状态栏高度、底部导航栏高度和导航指示器高度。通过调用getLastWindow获取窗口对象后,使用getWindowAvoidArea方法查询不同类型避让区域的矩形尺寸信息,并将结果输出到日志中。
·
可以使用window的[getWindowAvoidArea]方法获取,示例代码如下:
import { window } from '@kit.ArkUI';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct GetAvoidAreaHeight {
context = this.getUIContext();
build() {
Column() {
Button('GetAvoidAreaHeight')
.onClick(() => {
let type1 = window.AvoidAreaType.TYPE_SYSTEM;
let type2 = window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR;
window.getLastWindow(this.context.getHostContext()).then((data) => {
// Get the system default area, usually including the status bar and navigation bar
let avoidArea1 = data.getWindowAvoidArea(type1);
// Top status bar height
let statusBarHeight = avoidArea1.topRect.height;
// Bottom navigation bar height
let bottomNavHeight = avoidArea1.bottomRect.height;
// Get the navigation bar area
let avoidArea2 = data.getWindowAvoidArea(type2);
// Get the height of the navigation bar area
let indicatorHeight = avoidArea2.bottomRect.height;
console.info(`statusBarHeight is ${statusBarHeight}`);
console.info(`bottomNavHeight is ${bottomNavHeight}`);
console.info(`indicatorHeight is ${indicatorHeight}`);
}).catch((err: BusinessError) => {
console.error(`Failed to obtain the window. Cause: ${JSON.stringify(err)}`);
});
})
}
}
}
更多推荐



所有评论(0)