4

I do all my layout code via Java (no XML what-so-ever). However I found some peculiar behavior when setting an ImageViews height and width.

Whenever I try to set it's dimensions with .setMinimumHeight and .setMaximumHeight (using the same value), I find these values are almost never respected. The only time I get width and height to 'behave' is when I explictly set the values using getLayoutParams().width = whatever

My question is 2 parts:

  1. What is the point of having setMinimum and setMaximum if they are sporadically respected? What is the problem with using them?

  2. I find using layoutParams exhaustively verbose. I can of course override every class (a major chore since you can't Swizzle Java) but I'd like to avoid that.

Is there a way to get around having to use long lines like this

ViewGroup.LayoutParams imageParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);

_image.setLayoutParams(imageParams);

And get to this nice short syntax (without having to subclass everything, and hence my original desire to use .setMinimum/.setMaximum)

_image.setWidth(100);
Kevin Cronly
  • 1,388
  • 1
  • 13
  • 18
Aggressor
  • 13,323
  • 24
  • 103
  • 182
  • Have you tried calling `requestLayout()` after calling `setMinimumWidth()` and `setMinimumHeight()`? That said, I believe it's the responsibility of all `View` implementations to respect that value -- and not all do. ImageView does not seem to, as far as I can tell. – Kevin Coppock Jun 26 '15 at 16:54
  • Yes, I just ran requestLayout() again to make sure. Its as if I didn't try to set any values in the first place, setMin/setMax completely ignored. – Aggressor Jun 26 '15 at 16:57
  • Oh actually, those setters already request a layout, so that wouldn't have made a difference. So looking at the usages in `ImageView`, it may not be respected if the view has had `setAdjustViewBounds(true)` set. Try setting that to false. – Kevin Coppock Jun 26 '15 at 17:02
  • Even with false the value is still not respected. I also have found very bad behaviour when thats false, especially with button image scaling, so I always need to leave that to true. – Aggressor Jun 26 '15 at 17:04

0 Answers0