I add ImageView to layout in runtime, But ImageView is smaller after every loop. This is my code:
layout = (RelativeLayout)findViewById(R.id.layout_main);
width = getWindowManager().getDefaultDisplay().getWidth();
height = getWindowManager().getDefaultDisplay().getHeight(
for(int i=0; i< fruit.size();i++) {
int id = getApplicationContext().getResources().getIdentifier(fruit.get(i).file, "drawable",
getApplicationContext().getPackageName());
int h = random.nextInt(height);
int w = random.nextInt(width);
Logger.debug(TAG, "drawable/"+fruit.get(i).file+":"+id+":X="+w+":Y="+h);
ImageView imageView =new ImageView(getApplicationContext());
imageView.setImageResource(id);
imageView.setScaleX(0.3f);
imageView.setScaleY(0.3f);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(w,h,0,0);
imageView.setLayoutParams(layoutParams);
layout.addView(imageView);
And this is my result:
Please help me why and how to fix it? Thank you very much
More information: If I replace code
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(w,h,0,0);
imageView.setLayoutParams(layoutParams);
By
imageView.setY(h);
imageView.setX(w);
Result will