可以使用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)}`);
          });
        })
    }
  }
}
Logo

讨论HarmonyOS开发技术,专注于API与组件、DevEco Studio、测试、元服务和应用上架分发等。

更多推荐