Set ImageView width and height programmatically?

2021-6-3 anglehua

How can I set an ImageView's width and height programmatically?


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

imageView.getLayoutParams().height = 20;

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

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


采集自互联网,如有侵权请联系本人

Powered by emlog 京ICP备15036472号-3 sitemap