I've run into this situation two or three times, where I have some code that looks like this:
ViewGroup.LayoutParams params = myView.getLayoutParams();
params.height = 240;
myView.requestLayout();
Or alternatively:
ViewGroup.LayoutParams params = myView.getLayoutParams();
params.height = 240;
myView.setLayoutParams(params);
And the view size never changes. I've tried the following, with no luck:
- Calling
forceLayout, which ironically seems less forceful thanrequestLayout, because it doesn't notify the View's parents that a layout is needed. - Overriding
onMeasure()to see if it is ever called (it isn't). - Making sure that I'm not making the call to
requestLayoutduring a Layout pass.
I've also tried calling requestLayout on all of the View's parents, like so:
ViewParent parent = myView.getParent();
while (parent != null) {
parent.requestLayout();
parent = parent.getParent();
}
This works, but it seems really hacky. I'd much rather find the real solution.
What am I doing wrong?