Android imageview 不尊重 maxWidth?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3530328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:38:05  来源:igfitidea点击:

Android imageview not respecting maxWidth?

androidscalingimageview

提问by juell

So, I have an imageview that should display an arbitrary image, a profile picture downloaded from the internet. I want this the ImageView to scale its image to fit inside the height of the parent container, and a set max width of 60dip. However, if the image is tall ratio-wise, and doesn't need the full 60dip of width, the ImageView's width should decrease so the view's background fits snugly around the image.

所以,我有一个图像视图,它应该显示一个任意图像,一个从互联网下载的个人资料图片。我希望这个 ImageView 缩放它的图像以适应父容器的高度,并且设置的最大宽度为 60dip。但是,如果图像在比例方面很高,并且不需要完整的 60dip 宽度,则 ImageView 的宽度应该减小,以便视图的背景紧贴图像周围。

I tried this,

我试过这个,

<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"/>

but that made the ImageView super large for some reason, maybe it used the intrinsic width of the image and wrap_content to set it - anyway, it didn't respect my maxWidth attribute.. Does that only work inside some types of containers? It's in a LinearLayout...

但这使 ImageView 由于某种原因变得非常大,也许它使用了图像的固有宽度和 wrap_content 来设置它 - 无论如何,它不尊重我的 maxWidth 属性..这仅在某些类型的容器内有效吗?它在一个 LinearLayout 中......

Any suggestions?

有什么建议?

回答by juell

Ah,

啊,

android:adjustViewBounds="true"

is required for maxWidth to work.

需要 maxWidth 才能工作。

Works now!

现在工作!

回答by FeelGood

Setting adjustViewBoundsdoes not help if you use match_parent, but workaround is simple custom ImageView:

adjustViewBounds如果您使用match_parent,设置无济于事,但解决方法是简单的自定义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);
    }
}