HarmonyOS鸿蒙Next中开启深色模式,如何解决顶部显示内容被状态栏遮挡的问题

HarmonyOS鸿蒙Next中开启深色模式,如何解决顶部显示内容被状态栏遮挡的问题

【问题现象】

开启深色模式,顶部搜索框显示内容被状态栏覆盖,重启APP后恢复正常。

【背景知识】

window.getLastWindow获取的statusBarHeight高度是瞬时的。

【定位思路】

使用过程中开启、关闭深色模式都会有问题,覆盖原因是statusBarHeight获取的高度产生的影响。

let avoidArea = windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM);
let height = avoidArea.topRect.height;

getStatusBarHeight里面调用的系统方法返回了0,无法获取statusBarHeight的瞬时高度,需要获取瞬时高度使搜索框不被覆盖。

【解决方案】

window.getLastWindow获取的statusBarHeight高度是瞬时的,需要在onWindowStageCreate方法里使用windowClass.on监听窗口的变化,这样就能实时的获取statusBarHeight的高度。

代码示例如下:

// entryability.ets:
onWindowStageCreate(windowStage: window.WindowStage): void {

  let windowClass: window.Window = windowStage.getMainWindowSync(); // 获取应用主窗口
  try {
  windowClass.on('avoidAreaChange', (data) => {
  console.info('Succeeded in enabling the listener for system avoid area changes. type:' +
  JSON.stringify(data.type) + ', area: ' + JSON.stringify(data.area));
  let area = windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM);
  let statusBarHeight = 0
  // 注意:返回的是px值,如果要用vp值需要转换
  statusBarHeight = px2vp(area.topRect.height);
  PersistentStorage.persistProp('test', statusBarHeight);
});
} catch (exception) {
  console.error(`Failed to enable the listener for system avoid area changes. Cause code: ${exception.code}, message: ${exception.message}`);
}
windowStage.loadContent('pages/Index', (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.');
});
}
// Demo.ets:
import { window } from '@kit.ArkUI';

@Entry
@Component
struct Demo {
  @State message: string = 'Hello World';
  @StorageLink('test') statusBarHeight:number = 0

  build() {
    Column() {
      Row(){
        Text(this.message)
          .id('DemoHelloWorld')
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .alignRules({
            center: { anchor: '__container__', align: VerticalAlign.Center },
            middle: { anchor: '__container__', align: HorizontalAlign.Center }
          })
      }
      .padding({top: this.statusBarHeight})
      .width('100%')
      .backgroundColor(Color.Pink)
    }
    .height('100%')
    .width('100%')
  }
}

【总结】

window.getLastWindow获取的statusBarHeight高度是瞬时的,需要在onWindowStageCreate方法里使用了windowClass.on监听窗口的变化,才能正确获取到statusBarHeight的高度。


更多关于HarmonyOS鸿蒙Next中开启深色模式,如何解决顶部显示内容被状态栏遮挡的问题的实战教程也可以访问 https://www.itying.com/category-93-b0.html

1 回复

更多关于HarmonyOS鸿蒙Next中开启深色模式,如何解决顶部显示内容被状态栏遮挡的问题的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,开启深色模式后,顶部显示内容被状态栏遮挡的问题可以通过以下方式解决:

  1. onWindowStageCreate方法中,使用windowClass.on监听窗口的变化,实时获取statusBarHeight的高度。
  2. 通过window.getWindowAvoidArea获取系统避免区域的高度,并将其转换为vp值。
  3. 将获取到的statusBarHeight存储在PersistentStorage中,并在UI中使用该值进行布局调整。

具体代码示例如下:

// entryability.ets:
onWindowStageCreate(windowStage: window.WindowStage): void {
  let windowClass: window.Window = windowStage.getMainWindowSync();
  try {
    windowClass.on('avoidAreaChange', (data) => {
      let area = windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM);
      let statusBarHeight = px2vp(area.topRect.height);
      PersistentStorage.persistProp('test', statusBarHeight);
    });
  } catch (exception) {
    console.error(`Failed to enable the listener for system avoid area changes. Cause code: ${exception.code}, message: ${exception.message}`);
  }
  windowStage.loadContent('pages/Index', (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.');
  });
}

// Demo.ets:
import { window } from '@kit.ArkUI';

@Entry
@Component
struct Demo {
  @State message: string = 'Hello World';
  @StorageLink('test') statusBarHeight: number = 0

  build() {
    Column() {
      Row() {
        Text(this.message)
          .id('DemoHelloWorld')
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .alignRules({
            center: { anchor: '__container__', align: VerticalAlign.Center },
            middle: { anchor: '__container__', align: HorizontalAlign.Center }
          })
      }
      .padding({ top: this.statusBarHeight })
      .width('100%')
      .backgroundColor(Color.Pink)
    }
    .height('100%')
    .width('100%')
  }
}

通过以上方法,可以实时获取statusBarHeight的高度,并确保顶部内容不被状态栏遮挡。

回到顶部