调用getLocationOnScreen()
或getLocationInWindow()
都会得到一个大约30px(状态/通知栏的高度)太低的top/Y
坐标。left/X
坐标正对着。
正如我上面所暗示的,我认为不同之处在于状态/通知栏...我可能错了。我想如果我可以确定通知栏的大小,我就可以解决这个问题,但是,我在做这件事上遇到了麻烦。
任何帮助都将不胜感激。
发布于 2010-05-04 05:03:42
我最终通过如下方式确定状态/通知栏的高度来解决这个问题:
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;
发布于 2012-08-31 05:44:18
下面是我想要的获取状态栏高度和调整偏移的方法:
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);
发布于 2010-05-02 22:13:49
我也有同样的问题,试着使用
offset = myView.GetOffsetY();
并按该值调整Y坐标,例如
coordY -= offset;
提供-method的类:
class MyView extends View {
public int GetOffsetY() {
int mOffset[] = new int[2];
getLocationOnScreen( mOffset );
return mOffset[1];
}
}
https://stackoverflow.com/questions/2638342
复制相似问题