因此,我有一个图像视图,应该显示一个任意的图像,从互联网上下载的头像图片。我希望这个ImageView缩放它的图像以适应父容器的高度,并设置最大宽度为60。但是,如果图像在比例上是高的,并且不需要整个60度的宽度,那么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属性。这是否只在某些类型的容器中有效?它在一个LinearLayout里。
有什么建议吗?
发布于 2010-08-20 19:23:19
阿,
android:adjustViewBounds="true"
是maxWidth工作所必需的。
现在起作用了!
发布于 2015-07-17 02:29:06
如果使用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);
}
}
https://stackoverflow.com/questions/3530328
复制相似问题