ArkTs 获取屏幕宽高有两种方法
方法1 通过display 获取 【包括顶部状态栏的高度】
方法2 通过window 获取【不包括顶部状态栏的高度】
两种方法的区别在于是否包含顶部状态栏的高度
方法1
this.sWidth = display.getDefaultDisplaySync().width
this.sHeight = display.getDefaultDisplaySync().height
console.info('display width ' + this.sWidth)
console.info('display height ' + this.sHeight)
方法2
window.getLastWindow(getContext(this))
.then((widowClass: window.Window) => {
let windowProperties = widowClass.getWindowProperties();
this.sWidth = windowProperties.windowRect.width
this.sHeight = windowProperties.windowRect.height
console.info('widow width ' + this.sWidth)
console.info('widow height ' + this.sHeight)
})
.catch(() => {
})
两种方法获取的的宽高单位都是px