开发者

ImageView position and size

I have a ImageView defined in XML, which I later on inflate for use.

I want at one point to be the size 400,200 (width,height), at at another (500,300). Also I want to put it on specific place on the screen, something like setting x,y coordinates (or Rect(left, top, right, bottom) would be nice).

I understand for this I must 'play' with the LayoutParameters, but then I can't find any info how to specifica开发者_如何学Golly do it. Can anyone put few lines example just to understand this....


If you want to change the position of the imageview in your code dynamically you can only change the positions by setting their margins to move them left/right and up/down. The following receives the layout params of the imageviews so that you can change the views' margins.

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);


If you want to put in specified point of parent you can specify android:layout_marginLeft and android:layout_marginTop in px or dp. It not so clear, but working.
Programmly you can call method View.layout(int,int,int,int), but I cant find same XML parameter.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜