检索X&;android中按钮的Y坐标?

检索X&;android中按钮的Y坐标?,android,button,position,Android,Button,Position,我已经在安卓上工作了一段时间,想知道是否有可能在安卓上检索按钮的位置 我的目标是获取X&Y坐标并将其打印在LOGCAT上 请给我举个例子,我将不胜感激 谢谢当然,您可以获得这些,确保在尝试获取位置之前至少绘制一次视图。您可以尝试获取onResume()中的位置并尝试以下函数 view.getLocationInWindow() or view.getLocationOnScreen() 或者,如果您需要与父对象相关的内容,请使用 view.getLeft(), view.getTop() A

我已经在安卓上工作了一段时间,想知道是否有可能在安卓上检索按钮的位置

我的目标是获取X&Y坐标并将其打印在LOGCAT上

请给我举个例子,我将不胜感激


谢谢

当然,您可以获得这些,确保在尝试获取位置之前至少绘制一次视图。您可以尝试获取onResume()中的位置并尝试以下函数

view.getLocationInWindow()
or
view.getLocationOnScreen()
或者,如果您需要与父对象相关的内容,请使用

view.getLeft(), view.getTop()
API定义的链接:

如前所述,您可以使用来获取坐标x,y

以下是一个例子:

Button button = (Button) findViewById(R.id.yourButtonId);
Point point = getPointOfView(button);
Log.d(TAG, "view point x,y (" + point.x + ", " + point.y + ")");

private Point getPointOfView(View view) {
    int[] location = new int[2];
    view.getLocationInWindow(location);
    return new Point(location[0], location[1]);
}
奖励-获取给定视图的中心点:

Point centerPoint = getCenterPointOfView(button);
Log.d(TAG, "view center point x,y (" + centerPoint.x + ", " + centerPoint.y + ")");

private Point getCenterPointOfView(View view) {
    int[] location = new int[2];
    view.getLocationInWindow(location);
    int x = location[0] + view.getWidth() / 2;
    int y = location[1] + view.getHeight() / 2;
    return new Point(x, y);
}

我希望这个例子对某些人仍然有用。

getLocationInWindow(int[])和getLocationOnScreen(int[])都有参数。你确定你的想法吗?@hakkikonu看看我的答案,了解getLocationInWindow(int[])的用法。
buttonObj.getX();
buttonObj.getY();