Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think you need to read up in the documentation on <a href="http://developer.android.com/reference/android/view/View.html#onMeasure%28int,%20int%29" rel="nofollow">onMeasure</a> and see how it works.</p> <p>It is actually called multiple times depending on the type of layout container that the view is in, and the exact specifics will vary from one ViewGroup to another. Generally the dimension are loosest the first time around, and then get more fine grained as more components are laid out.</p> <p>When onMeasure is called, you first want to see what kind of MeasureSpec you have received by calling <code>MeasureSpec.getMode(widthMeasureSpec)</code>. This will return either <code>MeasureSpec.AT_MOST</code> or <code>MeasureSpec.EXACTLY</code>.</p> <p>If it returns AT_MOST, that tells you the largest possible area the view could fill at this time. When you're laying out the first view, it's likely saying that your first view can use at most the entire width of the screen, because no other components are laid out yet. Dividing the entire width of the container by two, you then tell the container that you're happy with using 50% of the horizontal layout by calling <code>setMeasuredDimension</code>.</p> <p>Next when <code>onMeasure</code> is called for your second view, only half of the horizontal width of the container is now available, because you've already requested half of the screen for the first view.</p> <p>You're then dividing that number by two, leaving the second view with only 25% of the horizontal width of the container, while the first view got 50% of the horizontal width.</p> <p>I'd suggest that overriding onMeasure is not the proper way to get a view to use half the screen, though I wanted to explain how it works. Is there some reason you think you can't achieve this with just standard xml layouts?</p> <p><strong>EDIT</strong>: Reread your question again, I think your problem is that you're assuming that widthMeasureSpec means the same thing as the screen width (which it does not). You could try to properly query the width of the screen and call <code>setMeasureDimension</code> based on that, though it might not work in every case. If you're not careful you may request a width greater than what's allotted to you based onMeasure. Or sometimes you will get called an <code>onMeasure</code> with <code>MeasureSpec.EXACTLY</code>, at which point you have no choice but to use the supplied width or height.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload