2016-06-19 56 views
0

我有一個佈局activity_main.xml如何獲得Button的座標?

<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,我怎麼能做到這一點。
感謝

回答

1

發生這種情況是因爲您在實際顯示視圖之前獲取了視圖的座標。

您可以使用View.post()方法,該方法在您的View正確啓動後執行一些代碼。

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

@ Vector88這是正確的,謝謝 –

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

@ milad.moafi做你試試吧。 – Ironman

+0

是的,它的回報爲0,第二個答案是正確的。順便一提。非常感謝您的關心 –

1

有幾件事情要考慮當你處理視圖座標:

  1. getLocationInWindow()返回一個窗口,它通常比你的根佈局更大的高度內的位置,因爲前者包括通知欄/應用程序/選項卡。

  2. getX(),getY(),getTop(),getLeft()如果您的視圖不是根佈局的直接子節點,則它們都會返回視圖父級中的位置,這可能無關緊要。

  3. 如果你的視圖的位置取決於另一個視圖的高度(例如它位於另一個視圖的下方,那麼誰的高度設置爲wrap_content),那麼只有在全局佈局完成後才能知道最終位置(使用OnGlobalLayoutListener)。

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