如何获取按钮坐标?

How get Button coordinate?

我有一个布局activity_main.xml

    <Button
        android:id="@+id/button"
        android:layout_marginTop="50dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal|top"
        android:text="@string/hover"/>

</FrameLayout>

如何获取按钮在屏幕上的坐标 (x, y)。现在我使用:

button.getX()
button.getY()

但是 return 0.0,我该如何实现。
谢谢

OnCreate() 方法中定义这个

Button button = (Button) findViewById(R.id.button);
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]);
}

发生这种情况是因为您在视图实际显示之前获取了视图的坐标。

您可以使用 View.post() 方法,该方法会在 View 正确初始化后执行一些代码。

mButton.post(
    new Runnable(){
        @Override
        public void run(){
            x = mButton.getX();
            y = mButton.getY();
        }
    }
);

处理视图坐标时需要考虑的几件事:

  1. getLocationInWindow() returns 在 window 中的位置通常比你的根布局有更高的高度,因为前者包括通知栏/应用栏/选项卡。

  2. getX(), getY(), getTop(), getLeft() 所有 return 在你的视图父级中的位置,如果你的视图不是直接的,这可能是无关紧要的根布局的子项。

  3. 如果你的视图的位置取决于另一个视图的高度(例如它位于另一个视图的下方,如果设置为wrap_content谁的高度),那么你将能够知道仅在全局布局完成后的最终位置(使用 OnGlobalLayoutListener)。

public class MainActivity extends Activity {

    Button b1, b2, b3, b4;

    int b1x1, b1x2, b1y1, b1y2;

    private TextView xcordview;
    private TextView ycordview;
    private TextView buttonIndicator;
    private RelativeLayout touchview;
    private static int defaultStates[];
    private Button mLastButton;
    private final static int[] STATE_PRESSED = {
            android.R.attr.state_pressed,
            android.R.attr.state_focused  
                    | android.R.attr.state_enabled };

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        xcordview = (TextView) findViewById(R.id.textView4);
        ycordview = (TextView) findViewById(R.id.textView3);
        buttonIndicator = (TextView) findViewById(R.id.button_indicator);
        touchview = (RelativeLayout) findViewById(R.id.relativelayout);

        b1 = (Button) findViewById(R.id.button1);
        b2 = (Button) findViewById(R.id.button2);
        b3 = (Button) findViewById(R.id.button3);
        b4 = (Button) findViewById(R.id.button4);
        defaultStates = b1.getBackground().getState();

    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();

        touchview.setOnTouchListener(new View.OnTouchListener() {

            private boolean isInside = false;

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                int x = (int) event.getX();
                int y = (int) event.getY();

                xcordview.setText(String.valueOf(x));
                ycordview.setText(String.valueOf(y));

                for (int i = 0; i < touchview.getChildCount(); i++) {
                    View current = touchview.getChildAt(i);
                    if (current instanceof Button) {
                        Button b = (Button) current;

                        if (!isPointWithin(x, y, b.getLeft(), b.getRight(), b.getTop(),
                                b.getBottom())) {
                            b.getBackground().setState(defaultStates);
                        }

                        if (isPointWithin(x, y, b.getLeft(), b.getRight(), b.getTop(),
                                b.getBottom())) {
                            b.getBackground().setState(STATE_PRESSED);
                            if (b != mLastButton) {
                                mLastButton = b;
                                buttonIndicator.setText(mLastButton.getText());
                            }
                        }

                    }
                }
                return true;
            }

        });

    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        // TODO Auto-generated method stub
        super.onWindowFocusChanged(hasFocus);
    }

    static boolean isPointWithin(int x, int y, int x1, int x2, int y1, int y2) {
        return (x <= x2 && x >= x1 && y <= y2 && y >= y1);
    }
}