2015-01-03 132 views
1

我有以下3个包装在RelativeLayout中(忽略可见性=“去”)。getLocationOnScreen返回错误的值

<ImageView 
    android:id="@+id/img1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/ic_launcher" 
    android:layout_marginLeft="10dip" 
    android:visibility="gone"/> 

<ImageView 
    android:id="@+id/img2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/ic_launcher" 
    android:layout_centerVertical="true" 
    android:layout_alignParentRight="true" 
    android:visibility="gone"/> 

<ImageView 
    android:id="@+id/img3" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/ic_launcher" 
    android:layout_marginLeft="60dip" 
    android:layout_alignParentBottom="true" 
    android:layout_marginBottom="100dip" 
    android:visibility="gone"/> 

当我打电话view.getLocationOnScreen(pos);

我得到pos[0]和219回0 pos[1]尽管他们都不是那些(X,Y)坐标。令人费解的是,每个ImageView都给了我相同的值,特别是考虑到它们位于不同的位置。这是为什么发生?

回答

2

在您的布局中,所有图像视图都处于“消失”状态。所以getLocationOnScreen()方法返回左上角的位置。

如果您想获取视图的确切位置,请根据您的要求将其可见性更改为“不可见”或“可见”。