本文介绍了来自 getLocationOnScreen/getLocationInWindow 的不正确坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

调用 getLocationOnScreen()getLocationInWindow() 都会给我一个 top/Y 坐标,大约 ~30px(状态/通知栏的高度)太低了.left/X 坐标是死的.

A call to getLocationOnScreen() or getLocationInWindow() both give me a top/Y coordinate that is about ~30px (status/notifications bar's height) too far down. The left/X coordinate is dead on.

正如我上面所暗示的,我相信差异是因为状态/通知栏......我可能是错的.如果我可以确定通知栏的大小,我想我可以解决这个问题,但是,我无法做到这一点.

As I hinted above, I believe the difference is because of the status/notification bar... I could be wrong. I think I can solve this if I can determine the size of the notification bar but, I'm having trouble doing just that.

任何帮助将不胜感激.

推荐答案

我最终通过像这样确定状态/通知栏的高度来解决这个问题:

I ended up solving this issue by determining the height of the status/notification bar like so:

View globalView = ...; // the main view of my activity/application

DisplayMetrics dm = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(dm);
int topOffset = dm.heightPixels - globalView.getMeasuredHeight();

View tempView = ...; // the view you'd like to locate
int[] loc = new int[2];
tempView.getLocationOnScreen(loc);

final int y = loc[1] - topOffset;

这篇关于来自 getLocationOnScreen/getLocationInWindow 的不正确坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 21:09