设置相对布局参数 [英] Setting relative layout parameters
问题描述
所以我想给的ImageView添加到我目前的XML设计 - 和它的工作体面。现在我的主要问题是,我似乎无法找到一种方法来设置图像的属性一样需要它显示等。
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 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);
我想瞎搞与setlayoutparams但得到完全不知道该怎么做了。
I tried messing around with setlayoutparams but got absolutely no clue what to do with it.
推荐答案
您确实有使用类的LayoutParams这样做,高效,方便地:
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?
这篇关于设置相对布局参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!