2011-08-12 43 views
0

我想在橫向模式下設置相對佈局中的圖像視圖的位置。Imageview定位錯誤

我正在使用Displaymetrics函數來獲取屏幕widt,然後調用setleft來設置名爲mStone1的圖像視圖的左側位置。

mMetrics = new DisplayMetrics(); 
getWindowManager().getDefaultDisplay().getMetrics(mMetrics); 

mStone1.setLeft(mMetrics.widthPixels-5); 

但我的應用程序有以下錯誤而崩潰: -

找不到方法android.widget.ImageView.setLeft,從方法com.example.Myapp.onCreate

VFY引用:無法解決虛擬方法11:Landroid/widget/ImageView; .setLeft(I)V

回答

0

此函數僅適用於API Level 11及更高版本。所以,除非你爲Android 3.0編寫(並運行應用程序),否則你不能使用這個函數。也許試試offsetLeftandRighthttp://developer.android.com/reference/android/view/View.html#offsetLeftAndRight(int

+0

是因爲它可能,View.setLeft()的目的,不是移動的視圖,但將其佈局(從API文檔): 設置該視圖相對於其父的左側位置。這個方法意味着被佈局系統調用,通常不應該被另外調用,因爲這個屬性可能隨時被佈局改變。 – Dan

+0

@jlindenbaum。謝謝你讓我知道問題的原因。但是offsetLeftandRight也不適合我 – Ruchira

+0

正如丹指出的那樣,它確實不是你想要的。看看While-E的帖子,這是一個更好的程序化佈局方法。 – jlindenbaum

0

你也可以試試View.Layout(int, int, int, int);功能,我相信。

public void layout (int l, int t, int r, int b)

Since: API Level 1

Assign a size and position to a view and all of its descendants

This is the second phase of the layout mechanism. (The first is measuring). In this phase, each parent calls layout on all of its children to position them. This is typically done using the child measurements that were stored in the measure pass().

Derived classes should not override this method. Derived classes with children should override onLayout. In that method, they should call layout on each of their children.

Parameters:

l Left position, relative to parent
t Top position, relative to parent
r Right position, relative to parent
b Bottom position, relative to parent

+0

這不適用於我。我嘗試了佈局函數來設置我的imageview相對於佈局的左側位置。 – Ruchira