HarmonyOS 鸿蒙Next获取WindowProperties.drawableRect值都是0

发布于 1周前 作者 itying888 来自 鸿蒙OS

HarmonyOS 鸿蒙Next获取WindowProperties.drawableRect值都是0

在EntryAbility.ts中,写如下代码
onWindowStageCreate(windowStage: window.WindowStage): void {

let windowClass = windowStage.getMainWindowSync()

let myproperty=windowClass.getWindowProperties()

let myrect=myproperty.drawableRect

}

得到的myrect,left,top,width,height都是0,看文档应该是得到可绘制的区域,是不是我哪里搞错了?



关于HarmonyOS 鸿蒙Next获取WindowProperties.drawableRect值都是0的问题,您也可以访问:https://www.itying.com/category-93-b0.html 联系官网客服。

1 回复

参考如下demo:

[@Entry](/user/Entry)
[@Component](/user/Component)
struct ScrollImagePage {
scroller: Scroller = new Scroller()
[@State](/user/State) winHeight: Length = ''
[@State](/user/State) viewHeight: Length = ''
[@State](/user/State) offsetY: Length = ''

aboutToAppear(): void { window.getLastWindow(getContext(this), (err, win) => { let prop = win.getWindowProperties() this.winHeight = prop.drawableRect.height // px }); }

build() { Scroll(this.scroller) { Column() { Text(‘第一个View’).height(400).backgroundColor(Color.Orange) Text(‘第二个View’).height(400).backgroundColor(Color.Green) Image($r(‘app.media.app_icon’)).height(400) .onAreaChange((oldValue: Area, newValue: Area) => { this.viewHeight = vp2px(Number(newValue.height)) // 将vp转成px if (newValue.globalPosition.y) { this.offsetY = vp2px(Number(newValue.globalPosition.y)) // 将vp转成px } }) Text(‘第三个View’).height(400).backgroundColor(Color.Blue) Text(‘文本’).height(400) } .width(‘100%’) } .onScrollStop(() => { // 91指的是导航条的高度,126指的是状态栏的高度,通过on(‘avoidAreaChange’)方法获取 let diff = Number(this.winHeight) - Number(this.viewHeight) - 91 if (this.offsetY > 126 && this.offsetY < diff) { console.log('onScrollStop offsetY ==> ’ + this.offsetY) } }) .scrollBar(BarState.Off) .width(‘100%’) .height(‘100%’) } }<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 8px; right: 8px; font-size: 14px;">复制</button>

回到顶部