android - 来自 getLocationOnScreen/getLocationInWindow 的坐标不正确

标签 android android-widget

getLocationOnScreen()getLocationInWindow() 的调用都给了我一个 top/Y 坐标约为 30px (status/通知栏的高度)太低了。 left/X 坐标是固定的。

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

任何帮助将不胜感激。

最佳答案

我最终通过确定状态/通知栏的高度来解决这个问题,如下所示:

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;

关于android - 来自 getLocationOnScreen/getLocationInWindow 的坐标不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2638342/

相关文章:

java - Android 主屏幕小部件

android - 如何在 LibGDX 中创建图形化的 Android Widget

android - 如何从aws s3下载图片到imageview?

Android:像股票浏览器一样的 ActionBar

android - ContentLoadingProgressBar 的示例用法

android - 如何在 Debug模式下,在 react-native-firebase 中禁用 Crashlytics?

android - 未通知多个 BluetoothGattCharacteristic

python - 如何使用 python 制作 android 小部件?

Android:交换2个 View 的位置

android - 从 Assets 文件夹中读取图像文件