layout中设置图片自适应大小,并且设置最大宽高,当图片的宽高大于设置的最大值时,宽高值为设置的最大值。
view plaincopy to clipboardprint?
<ImageView Android:
/>
关键代码:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:maxWidth="42dp"
android:maxHeight="42dp"
代码
ImageView iv = new ImageView(...); iv.setImageResource(...); iv.setScaleType(ImageView.ScaleType.FIT_XY); iv.setAdjustViewBounds(true); iv.setMaxHeight(130);//高度 iv.setMaxWidth(130);//宽度 return iv;