设置相对布局参数

时间:2010-07-21 12:48:27

标签: android image layout

所以我正在尝试将imageview添加到我当前的xml设计中 - 并且它的工作正常。现在我的主要问题是我似乎无法找到一种方法来设置图像的属性,如需要显示的位置等。

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,但完全不知道如何处理它。

1 个答案:

答案 0 :(得分:2)

您实际上必须使用LayoutParams类来高效且轻松地执行此操作:

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

这适用于我,并将我的图标放在屏幕的左上角,并在屏幕两侧显示指定的边距。 这有帮助吗?