0
我想在布局中放置一些图像(在点击操作时触发)。我必须将它们放置成不会脱离父级布局。我使用上一下布局添加新的图像在布局中放置图像android
代码:
LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
ImageView image = new ImageView(this);
LinearLayout.LayoutParams coordinates = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
image.setLayoutParams(coordinates);
image.setImageResource(R.drawable.image);
layout.addView(image);
如果我按上的布局,我要看到我的ImageView放随机。
Random random = new Random();
int x = random.nextInt(layout.getWidth());
int y = random.nextInt(layout.getHeight());
image.setX(x);
image.setY(y);
但是这不行。我也看到了我的布局外的那些图像。
你在用什么“Random”? – mrid
这是在点击动作,我想随机将图像放在我的布局,只要我点击布局。 – simplify