Onmeasure() Called With Exactly And Spec Size 0
Solution 1:
Since I finally understood what's going on, I leave here the answer for future reference:
Android is doing what it has to do when measuring the UI components.
If the user (me in this case) does not follow the simple rules, then EXACTLY 0 may happen.
It can be made harmless if you just check for 0 size in the onSizeChanged()
method. But even better if you avoid mixing measure modes, like I did. Explanation follows.
I defined in XML weighted views (using layout_weight
). Those were the custom views mentioned in the question. My mistake was to also try to require a specific height for the views in
@OverrideprotectedvoidonMeasure(int widthMeasureSpec, int heightMeasureSpec)
the culprit are the lines
int desiredHeight = Math.max(BOX_MIN_HEIGHT, HSVColorPickerPreference.this.boxHeight);
. . .
chosenHeight = Math.min(specHeight, desiredHeight);
. . .
This clashes head to head with the heuristic for weighted layout. Why? Let's take for example 3 widgets with weight=1, and one of them is behaving bad, as described above.
When LineraLayout makes a first pass over its children, it lets them get wild and ask for any size they want. In our example 2 widgets will ask for as much as possible, the custom widget will ask for something modest, less than the maximum.
The second pass is the killer, LinearLayout does not know one of the weighted widgets asked for less than it is supposed to, all in all, it has a weight defined for it. LinearLayout looks at the total requested measure form pass one and sees it is more than it has to give. Then it calculates the delta overflow and makes another pass distributing the overflow among the weighted widgets. Consequently the custom view widget has to cut back more than it requested, leaving it at 0 size.
The situation is analog to going for a beer with friends. You order one beer and your friends order beer, fries, chasers, the lot. At the end of the evening the check is split equally among everybody, you end up paying more than the beer you had. So did my custom view.
Post a Comment for "Onmeasure() Called With Exactly And Spec Size 0"