- ImageView布局加上android:adjustViewBounds="true"
-
<ImageView android:id="@+id/shopcart_iv" android:layout_width="match_parent" android:layout_height="wrap_content" android:scaleType="fitXY" android:layout_centerHorizontal="true" android:layout_alignParentBottom="true" android:background="@drawable/ic_merchant_logo" android:adjustViewBounds="true" />
在代码中:
//holder.shopcart_iv为imageview控件
int screenWidth = ManagerUtils.getWeight(context);
ViewGroup.LayoutParams lp = holder.shopcart_iv.getLayoutParams();
lp.width = screenWidth;
lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
holder.shopcart_iv.setLayoutParams(lp);
holder.shopcart_iv.setMaxWidth(screenWidth);
holder.shopcart_iv.setMaxHeight(screenWidth * 8);
public static int getWeight(Context context){
WindowManager wm = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
int width = wm.getDefaultDisplay().getWidth();
int height = wm.getDefaultDisplay().getHeight();
return width;
}