Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android imageview not respecting maxWidth?

Ah,

android:adjustViewBounds="true"

is required for maxWidth to work.

Works now!


Setting adjustViewBounds does not help if you use match_parent, but workaround is simple custom 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);
    }
}