Android 来自 getLocationOnScreen/getLocationInWindow 的不正确坐标
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2638342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Incorrect Coordinates From getLocationOnScreen/getLocationInWindow
提问by nimph
A call to getLocationOnScreen()or getLocationInWindow()both give me a top/Ycoordinate that is about ~30px (status/notifications bar's height) too far down. The left/Xcoordinate is dead on.
调用getLocationOnScreen()或getLocationInWindow()两者都给我一个top/Y大约 ~30px(状态/通知栏的高度)太远的坐标。该left/X坐标上死了。
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.
正如我上面暗示的那样,我相信差异是因为状态/通知栏......我可能是错的。如果我可以确定通知栏的大小,我想我可以解决这个问题,但是,我在这样做时遇到了麻烦。
Any help would be greatly appreciated.
任何帮助将不胜感激。
回答by nimph
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;
回答by satur9nine
Here is how I like to get the status bar height, and adjust the offset:
这是我喜欢获取状态栏高度并调整偏移量的方式:
final int[] location = new int[2];
anchor.getLocationInWindow(location); // Includes offset from status bar, *dumb*
Rect anchorRect = new Rect(location[0], location[1],
location[0] + anchor.getWidth(), location[1] + anchor.getHeight());
anchor.getRootView().findViewById(android.R.id.content).getLocationInWindow(location);
int windowTopOffset = location[1];
anchorRect.offset(0, -windowTopOffset);
回答by user330844
I am having the same problem, try using
我有同样的问题,尝试使用
offset = myView.GetOffsetY();
and adjust your Y coord by that value, e.g.
并通过该值调整您的 Y 坐标,例如
coordY -= offset;
The class which offers the ``-method:
提供 ``-method 的类:
class MyView extends View {
public int GetOffsetY() {
int mOffset[] = new int[2];
getLocationOnScreen( mOffset );
return mOffset[1];
}
}
回答by groucho
This answer does not include how to get the status bar height, but it does explain the behavior of getLocationOnScreen()and getLocationInWindow()returning the same value.
这个答案并不包括如何让状态栏的高度,但它确实解释的行为getLocationOnScreen(),并getLocationInWindow()返回相同的值。
In the case of a normal activity (not a dialog) you should expect these two methods to return the same value. A Window lays out underneath (as in z-order not y coordinates) the status bar, so these methods cannot be used to determine the height of the status bar.
在正常活动(不是对话框)的情况下,您应该期望这两种方法返回相同的值。窗口在状态栏下方(如 z 顺序而不是 y 坐标)布置,因此这些方法不能用于确定状态栏的高度。

