简体   繁体   中英

getLocationOnScreen of a child of GridLayout

I'm attempting to get the x/y of an imageview within a GridLayout but my log keeps showing X & Y: 0 0 any ideas?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/root" >

    ...

    <GridLayout
        android:id="@+id/gridLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:columnCount="3"
        android:rowCount="5" >

        ...

        <ImageView
            android:id="@+id/fivetwo"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:contentDescription="@string/gameboard"
            android:gravity="center"
            android:src="@drawable/tile" 
            android:layout_columnSpan="1"
            android:layout_rowSpan="1"  />

heres the java:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_level);
....
ImageView temp = (ImageView) findViewById(R.id.fivetwo);
            int originalPos[] = new int[2];
            temp.getLocationOnScreen( originalPos );
            Log.i(TAG, "X & Y: " + originalPos[0] + " " + originalPos[1]);

...

Remember: getX() and getY() return 0 if components are not drawn yet (in onCreate(){} ) .


To find out the position of a view as soon as it is ready:

Add a tree observer to the layout. This should return the correct position. onCreate is called before the layout of the child views are done. So the width and height is not calculated yet. To get the height and width. Put this on the onCreate method

final ImageView temp = (ImageView) findViewById(R.id.fivetwo);
ViewTreeObserver vto = temp.getViewTreeObserver(); 
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
    @Override 
    public void onGlobalLayout() { 
        temp.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
        int x  = temp.getX();
        int y = temp.getY();
        Log.v(TAG, String.format("X:%d Y:%d",x,y);
    } 
});

You need to wait for the callback when the layout has placed the children views. The code you are using returns 0 because the position is returned before the layout is placed. Use this code:

temp.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        getViewTreeObserver().removeGlobalOnLayoutListener(this);

        int[] locations = new int[2];
        temp.getLocationOnScreen(locations);
        int x = locations[0];
        int y = locations[1];
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM