调用 getLocationOnScreen()
或 getLocationInWindow()
方法,它们都会给我一个关于top/Y
坐标的结果,但是它距离实际位置(状态栏/通知栏高度)偏低了大约30像素。而left/X
坐标则完全正确。
正如我上面提到的,我认为差异是由于状态栏/通知栏造成的...但我可能错了。如果我能确定通知栏的大小,我认为我可以解决这个问题,但是我现在遇到了麻烦。
任何帮助将不胜感激。
调用 getLocationOnScreen()
或 getLocationInWindow()
方法,它们都会给我一个关于top/Y
坐标的结果,但是它距离实际位置(状态栏/通知栏高度)偏低了大约30像素。而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;
这是我获取状态栏高度、并调整偏移量的方法:
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);
offset = myView.GetOffsetY();
并通过该值调整您的Y坐标,例如:
coordY -= offset;
class MyView extends View {
public int GetOffsetY() {
int mOffset[] = new int[2];
getLocationOnScreen( mOffset );
return mOffset[1];
}
}
这个答案并不包括如何获取状态栏的高度,但是它解释了getLocationOnScreen()
和getLocationInWindow()
返回相同值的行为。
对于普通活动(而不是对话框),您应该期望这两种方法返回相同的值。窗口在状态栏下面布局(按z顺序而不是y坐标),因此这些方法不能用于确定状态栏的高度。
正如 @ThammeGowda 所说,当键盘显示时,正确答案将给出错误的位置,但您仍然需要计算操作栏的顶部偏移量,因此直接获取操作栏的大小即可
fun View.getPosition(): Point {
val tv = TypedValue()
if (context.theme.resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
val actionBarHeight =
TypedValue.complexToDimensionPixelSize(tv.data, resources.displayMetrics)
val loc = IntArray(2).apply { getLocationInWindow(this) }
val newY = loc[1] - actionBarHeight - (measuredHeight / 2) // to get the position in the middle of the view
return Point(loc[0], newY)
}
return Point(0,0)
}
void setOffset(){
int[] point1=new int[2];
view1.getLocationInWindow(point1);
int[] point2=new int[2];
view2.getLocationInWindow(point2);
int leftOffset=point2[0]-point1[0];
int topOffset=point2[1]-point1[1];
// no need to call this function more than once
// set the offset values in global variables
// remenber do't call it in OnCreate method
// this helps to mesure all type of screen size
}
void setLocationOfView(){
// now do your animation or any view to move
int[] point =new int[2];
sourceView.getLocationInWindow(point);
view.setX(point[0]-leftOffset);
view.setY(point[1]-topOffset);
//wow you did it i am glad to help you and sorry for being late
}
解决方案:
android:layout_marginTop="0dp"
获取不可见PNG文件在屏幕上的位置。这将是您的y坐标错误的偏移量。
int[] location = new int[2];
invisiblepng.getLocationOnScreen(location);
int sourceY = location[1];
SharedPreferences.Editor editor = sp.edit();
editor.putString("compensateybug", Integer.toString(sourceY));
editor.apply();
and
public static Point getLocationOnScreen(View view) {
int[] location = new int[2];
view.getLocationOnScreen(location);
return new Point(location[0], location[1]);
}
检索“状态/通知栏”的Y坐标值。
String compensateybug = sp.getString("compensateybug", "0");
int yourycoordinate = parseInt(compensateybug);
我已经附上了一个用于下载的不可见的PNG图像文件:
https://drive.google.com/file/d/1zxhP1r6ISvKG8Ig8iItDY2x6k5RVo4Nu/view?usp=sharing
int[] locInWindow = new int[2];
//创建一个长度为2的整型数组locInWindow
globalView.getLocationInWindow(locInWindow);
//获取globalView
在窗口中的坐标并存储在locInWindow
数组中topOffset = locInWindow[1];
//通过获取globalView
的Y坐标来计算topOffset
- Thamme Gowda