为什么我的getY()返回0?

时间:2014-07-27 02:14:20

标签: java android

我试图通过onClickListener()将ImageView移动到另一个ImageView的相同位置我可以返回x而不是y。

我要复制的视图位置在xml的不同布局中,一个是相对布局,另一个是相对布局中的线性。

是因为它根据布局而不是屏幕边缘的位置检查y位置?

public class LevelTwo extends Activity {


ImageView iv1,iv2,iv3;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);




    iv1 = (ImageView) findViewById(R.id.imageView1);
    iv2 = (ImageView) findViewById(R.id.imageView2);


    iv1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub


            iv1.setY(iv2.getY());
            iv1.setX(iv2.getX());





        }
    });

}


}

xml布局是这样的

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >





    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:src="@drawable/wtsa" 
        android:layout_below="@+id/layout1"

        />


<LinearLayout
        android:id="@+id/layout1" 
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:orientation="horizontal"
        android:gravity="center"
        >

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="55dp"
            android:layout_height="55dp"
            android:src="@drawable/wtsblank" 
            />

1 个答案:

答案 0 :(得分:2)

  

是因为它根据布局而不是屏幕边缘的位置检查y位置?

是的,你是对的。

getY()返回此视图的视觉y位置(以像素为单位)。这相当于translationY属性加上当前的top属性。 top属性是此视图相对于其父级的顶部位置。

相关问题