最近碰到一个需求,要求是在不知道图片宽度和高度的状况下,让图片在指定宽度内充满,同时高度自适应,在网络上查找了一下,也有不少解决方法,后来针对本身的应用,选择了一个修改较小的方案,最后证实效果仍是蛮不错的,记录在这里,但愿能帮助到有一样需求的人。android
首先,须要给你的ImageView布局加上android:adjustViewBounds="true"网络
<ImageView android:id="@+id/iv_custom_showdress_item_dress" android:layout_width="fill_parent" android:layout_height="wrap_content" android:adjustViewBounds="true"/>
而后,在代码里设置ImageView.最大宽度和最大高度,由于adjustViewBounds属性只有在设置了最大高度和最大宽度后才会起做用布局
WindowManager wm = (WindowManager) _context.getSystemService(Context.WINDOW_SERVICE); int screenWidth = wm.getDefaultDisplay().getWidth(); ViewGroup.LayoutParams lp = viewHolder.showDress.getLayoutParams(); lp.width = screenWidth; lp.height = LayoutParams.WRAP_CONTENT; viewHolder.showDress.setLayoutParams(lp);