开发者

Setting relative layout parameters

开发者 https://www.devze.com 2023-01-08 10:23 出处:网络
So I\'m trying to add an imageview to my current xml design - and its working decently. Now my main problem is that I cant seem to find a way to set the attributes for the image like where it开发者_St

So I'm trying to add an imageview to my current xml design - and its working decently. Now my main problem is that I cant seem to find a way to set the attributes for the image like where it开发者_StackOverflow社区 needs to be displayed etc.

RelativeLayout mRelativeLayout = (RelativeLayout) findViewById(R.id.board);

ImageView i = new ImageView(this);
i.setImageResource(R.drawable.blue_1);
i.setAdjustViewBounds(true);
mRelativeLayout.addView(i);
setContentView(mRelativeLayout);

I tried messing around with setlayoutparams but got absolutely no clue what to do with it.


You actually have to use the class LayoutParams to do that efficiently and easily :

RelativeLayout mRelativeLayout = (RelativeLayout) findViewById(R.id.board);
ImageView i = new ImageView(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(40, 40);
params.leftMargin = 25;
params.topMargin = 25;
i.setImageResource(R.drawable.icon);
i.setAdjustViewBounds(true);
mRelativeLayout.addView(i, params);

This works for me and put my icon in the top left corner of the screen with the specified margin to the sides of the screen. Does that help?

0

精彩评论

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