2011-07-18 263 views
0

我有一個在XML中定義的ImageView,我稍後會使用它來膨脹。ImageView的位置和大小

我想在一個點上的大小400,200(寬度,高度),在另一個(500,300)。 另外我想把它放在屏幕上的特定位置,像設置x,y座標(或Rect(左,上,右,下)會很好)。

我明白這一點,我必須'與'LayoutParameters'玩',但我找不到任何信息如何專門做到這一點。 任何人都可以放幾行例子來理解這個......

+0

請在您的代碼中指定您撥打電話的位置。 – cdhabecker

回答

0

如果你想放在父指定的點,你可以在px或dp中指定android:layout_marginLeftandroid:layout_marginTop。它不是很清楚,但工作。
Programmly你可以打電話methodView.layout(int,int,int,int),但我不能找到相同的XML參數。

+0

是的,我需要它編程..我用scaleType「fitXY」定義ImageView,然後做iv.setMargins(100,100,0,0) - 期望它將下降100像素,左側100像素。它不會從頂點移動... – Pett

+0

您可以使用'View.layout(int l,int t,int r,int b)'方法。反對[文檔](http://developer.android.com/reference/android/view/View.html#layout%28int,%20int,%20int,%20int%29) –

1

如果您想要動態更改代碼中的圖像視圖的位置,您只能通過設置邊距來左右/上移/下移位置來更改位置。以下內容將接收圖像視圖的佈局參數,以便您可以更改視圖的邊距。

ImageView image; 
// Gets the layout params of the imageview 
MarginLayoutParams params = (MarginLayoutParams)image.getLayoutParams(); 
// Sets the margins of the imageview, making it move to a specific location on the screen 
param.setMargins(int left, int top, int right, int bottom);