Android的ImageView不尊重maxWidth?

所以,我有一个imageview应该显示一个任意图像,从互联网上下载的个人资料图片。 我想这ImageView缩放其图像,以适应父容器的高度,并设置最大宽度60dip。 但是,如果图像比例高,并且不需要完整的60dip宽度,则ImageView的宽度应该减小,以便视图的背景能够贴合图像。

我试过这个,

<ImageView android:id="@+id/menu_profile_picture" android:layout_width="wrap_content" android:maxWidth="60dip" android:layout_height="fill_parent" android:layout_marginLeft="2dip" android:padding="4dip" android:scaleType="centerInside" android:background="@drawable/menubar_button" android:layout_centerVertical="true"/> 

但由于某种原因,ImageView超大,也许它使用图像的内在宽度和wrap_content来设置它 – 无论如何,它不尊重我的maxWidth属性..是否只适用于某些types的容器? 它在LinearLayout中…

有什么build议么?

啊,

 android:adjustViewBounds="true" 

maxWidth是必需的。

现在工作!

如果您使用match_parent ,则设置adjustViewBounds无助于您,但解决方法很简单自定义ImageView

 public class LimitedWidthImageView extends ImageView { public LimitedWidthImageView(Context context) { super(context); } public LimitedWidthImageView(Context context, AttributeSet attrs) { super(context, attrs); } public LimitedWidthImageView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int specWidth = MeasureSpec.getSize(widthMeasureSpec); int maxWidth = getMaxWidth(); if (specWidth > maxWidth) { widthMeasureSpec = MeasureSpec.makeMeasureSpec(maxWidth, MeasureSpec.getMode(widthMeasureSpec)); } super.onMeasure(widthMeasureSpec, heightMeasureSpec); } }