鸿蒙应用获取状态栏宽高的方法(用来解决计算应用在状态栏高度的问题)

 

async onWindowStageCreate(windowStage: window.WindowStage): Promise {
  // Main window is created, set main page for this ability
  hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');

  windowStage.loadContent('pages/Login', (err) => {
    if (err.code) {
      hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
      return;
    }
    hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.');
  });

  const windowInformation = await windowStage.getMainWindow()
  //设置全屏
  windowInformation.setWindowLayoutFullScreen(true)
//获取状态栏高度
  const stateHeight=windowInformation.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM)
  const topHeight=stateHeight.topRect.height //单位是px
  AppStorage.setOrCreate('topHeight',px2vp(topHeight))//存储状态栏高度给页面使用
}

你可能感兴趣的:(harmonyos,华为)