我有一個放置在屏幕中心的ImageView(爲此我使用了android:layout_centerInParent="true"
)。但是當我打電話給方法getX()
和getY()
我收到他們兩個0(零)。(0,0)放置在屏幕中間的視圖座標
我認爲問題是xml屬性android:layout_centerInParent="true"
。即它實際上並沒有改變屏幕上視圖的位置。
這裏的XML文件內容:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/fundal">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/imageView"
android:src="@drawable/playbtn"
android:layout_centerInParent="true" />
</RelativeLayout>
這裏就是我所說的方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drag);
ImageView img =(ImageView)findViewById(R.id.imageView);
Log.d("DEBUG_TAG", "Coordinates: "+img.getX()+" "+img.getY());
}
請張貼的代碼xml佈局和您要調用的代碼getX() – lelloman