2011-08-26 15 views

Respuesta

9

Claro, usted puede conseguir éstos, asegúrese de que las vistas son atraídos al menos una vez antes de tratar de obtener las posiciones. Se podría tratar de obtener las posiciones en onResume() y tratar estas funciones

view.getLocationInWindow() 
or 
view.getLocationOnScreen() 

o si necesita algo en relación con el padre, utilizar

view.getLeft(), view.getTop() 

Enlaces a las definiciones del API:

+0

** getLocationInWindow (int []) ** y ** getLocationOnScreen (int []) ** tiene el parámetro. ¿Estás seguro de tu idea? – hakiko

+0

@hakkikonu eche un vistazo a mi respuesta para comprender el uso de ** getLocationInWindow (int []) **. –

4

Como Azlam dijo que se puede utilizar para obtener los View.getLocationInWindow()coordenadas x, y.

Aquí se muestra un ejemplo:

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

Bono - Para obtener el punto central de la vista dada:

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

espero que el ejemplo todavía puede ser útil a alguien.

0
buttonObj.getX(); 
buttonObj.getY(); 
Cuestiones relacionadas