Set ImageView width and height programmatically?

我如何以编程方式设置ImageView的宽度和高度?


It may be too late but for the sake of others who have the same problem, to set the height of the ImageView:

image_view.getLayoutParams().height = 20;

Hope this helps.

Important. If you're setting the height after the layout has already been 'laid out', make sure you also call:

image_view.requestLayout()

If your image view is dynamic, the answer containing getLayout will fail with null-exception.

In that case the correct way is:

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(100, 100);
iv.setLayoutParams(layoutParams);

This simple way to do your task:

setContentView(R.id.main);    
ImageView iv = (ImageView) findViewById(R.id.left);
int width = 60;
int height = 60;
LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(width,height);
iv.setLayoutParams(parms);

and another way if you want to give screen size in height and width then use below code :

setContentView(R.id.main);    
Display display = getWindowManager().getDefaultDisplay();
ImageView iv = (LinearLayout) findViewById(R.id.left);
int width = display.getWidth(); // ((display.getWidth()*20)/100)
int height = display.getHeight();// ((display.getHeight()*30)/100)
LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(width,height);
iv.setLayoutParams(parms);

hope use full to you.

链接地址: http://www.djcxy.com/p/36744.html

上一篇: AndEngine不适用于摩托罗拉Defy

下一篇: 以编程方式设置ImageView宽度和高度?