如何获得Button协调?

时间:2016-06-19 11:25:01

标签: android android-view

我有一个布局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>

如何在屏幕上显示Button的坐标(x,y)。现在我用:

button.getX()
button.getY()

return 0.0,我怎样才能实现这一点 感谢

4 个答案:

答案 0 :(得分:3)

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]);
}

答案 1 :(得分:1)

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

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

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

答案 2 :(得分:1)

处理视图坐标时需要考虑的一些事项:

  1. getLocationInWindow()返回窗口中的位置,该窗口的高度通常比根布局高,因为前者包含通知栏/ appbar / tabs。

  2. getX(),getY(),getTop(),getLeft()都返回视图父级中的位置,如果您的视图不是root的直接子级,则可能无关紧要布局。

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

答案 3 :(得分:0)

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);
    }
}