#鸿蒙学习大百科#如何获取当前设备时sm,md,lg哪个尺寸的?

如何获取当前设备时sm,md,lg哪个尺寸的?

HarmonyOS
2024-09-26 09:53:55
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
重庆大镖客

EntryAbility.ets下。

onWindowStageCreate(windowStage: window.WindowStage): void {
  windowStage.getMainWindow().then((data: window.Window) => {
    this.windowObj = data;
    this.updateBreakpoint(this.windowObj.getWindowProperties().windowRect.width,
      this.windowObj.getWindowProperties().windowRect.height);
    this.windowObj.on('windowSizeChange', (windowSize: window.Size) => {
      this.updateBreakpoint(windowSize.width, windowSize.height);
    })
  })
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
private windowObj?: window.Window;
private updateBreakpoint(windowWidth: number, windowHeight: number): void {
  let windowWidthVp = windowWidth / display.getDefaultDisplaySync().densityPixels;
  let curBp: string = '';
  if (windowWidthVp < 600) {
    curBp = 'sm';
  } else if (windowWidthVp < 840) {
    curBp = 'md';
  } else {
    curBp = 'lg';
  }
  AppStorage.setOrCreate('breakPoint', curBp);
  AppStorage.setOrCreate('windowSize', windowWidth / display.getDefaultDisplaySync().densityPixels);
  AppStorage.setOrCreate('windowSizeHeight', windowHeight / display.getDefaultDisplaySync().densityPixels);
};
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
分享
微博
QQ
微信
回复
2024-09-26 16:23:31
相关问题