Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I ended up figuring it out through some significant Googling. On <a href="http://translate.google.com/translate?hl=en&amp;sl=zh-CN&amp;tl=en&amp;u=http://cafebabe.sinaapp.com/?cat=16">this page</a> I found out that <strong>onMeasure is actually called twice</strong> when <code>android:layout_weight</code> is set. I found out later that it's mentioned in <a href="http://developer.android.com/guide/topics/ui/how-android-draws.html">How Android Draws Views</a> that <code>measure()</code> can be called any number of times, it just didn't immediately sit in my brain. The first pass obviously can't give a value for the size of each child, since it doesn't know what the weights of all of the View's siblings are. It goes through it once and gives a width of 0 with a MeasureSpec.UNSPECIFIED to see if the child has any specific constraints, then goes through <em>again</em> to assign the actual weights with a MeasureSpec.EXACTLY. My Activity was getting screwed up at the first pass, so it never got to the layout step. Amending my onMeasure() to the following code fixed the problem.</p> <pre><code>@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int widthMode = MeasureSpec.getMode(widthMeasureSpec); if (widthMode != MeasureSpec.UNSPECIFIED) { int widthSize = MeasureSpec.getSize(widthMeasureSpec); int lineWidth = widthSize - getPaddingLeft() - getPaddingRight(); // Change the text size until the largest line of text fits. while (lineWidth &lt; calculateTextWidth()) { for (TextView textView : this.childViews) { float newSize = textView.getTextSize() - 1; textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, newSize); } } } super.onMeasure(widthMeasureSpec, heightMeasureSpec); } </code></pre>
 

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