title: Measuring children in custom view breaks in RTL
body: <p>I have a custom view that extends LinearLayout and implements onMeasure. I'd like the children to be either as wide as they need to be or filling the available space.</p>
  <p>XML files:
  Parent:</p>
  <pre class="lang-xml prettyprint-override"><code>&lt;FrameLayout
      xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;&gt;
  
      &lt;com.example.myapplication.AtMostLinearLayout
          android:id=&quot;@+id/at_most_linear_layout&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;wrap_content&quot; /&gt;
  
  &lt;/FrameLayout&gt;
  </code></pre>
  <p>Button example:</p>
  <pre><code>&lt;FrameLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      android:layout_width=&quot;wrap_content&quot;
      android:layout_height=&quot;wrap_content&quot;&gt;
  
      &lt;ImageView
          android:layout_width=&quot;48dp&quot;
          android:layout_height=&quot;48dp&quot;
          android:layout_gravity=&quot;center&quot;
          android:src=&quot;@android:drawable/ic_delete&quot; /&gt;
  &lt;/FrameLayout&gt;
  </code></pre>
  <p>Views are added programmatically, for example:</p>
  <pre class="lang-kotlin prettyprint-override"><code>   findViewById&lt;AtMostLinearLayout&gt;(R.id.at_most_linear_layout).apply {
              repeat(4) {
                  LayoutInflater.from(context).inflate(R.layout.button, this)
              }
          }
  </code></pre>
  <p>Finally the Custom view class:</p>
  <pre class="lang-kotlin prettyprint-override"><code> 
  class AtMostLinearLayout @JvmOverloads constructor(
      context: Context,
      attrs: AttributeSet? = null,
      defStyle: Int = 0
  ) : LinearLayout(context, attrs, defStyle) {
      private val maxTotalWidth = context.resources.getDimensionPixelOffset(R.dimen.max_buttons_width)
      init {
          orientation = HORIZONTAL
      }
  
      override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
          super.onMeasure(widthMeasureSpec, heightMeasureSpec)
          if (childCount &lt; 1) return
  
          val newWidth = min(measuredWidth, maxTotalWidth)
          var availableWidth = newWidth
          var numberOfLargeChildren = 0
          repeat(childCount) {
              getChildAt(it).let { child -&gt;
                  if (child.measuredWidth &gt; availableWidth / childCount) {
                      availableWidth -= child.measuredWidth
                      numberOfLargeChildren++
                  }
              }
          }
  
          val minChildWidth = availableWidth / max(childCount - numberOfLargeChildren, 1)
          repeat(childCount) {
              getChildAt(it).apply {
                  measure(
                      MeasureSpec.makeMeasureSpec(max(measuredWidth, minChildWidth), EXACTLY),
                      UNSPECIFIED
                  )
              }
          }
  
          setMeasuredDimension(
              makeMeasureSpec(newWidth, EXACTLY), makeMeasureSpec(measuredHeight, EXACTLY))
      }
  }
  </code></pre>
  <p>It works fine in LTR: <a href="https://i.stack.imgur.com/VmxJl.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VmxJl.png" alt="Left to right screenshot" /></a>
  In RTL however the views are off set for some reason and are drawn outside the ViewGroup: <a href="https://i.stack.imgur.com/67vZ6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/67vZ6.png" alt="Right to left screenshot" /></a></p>
  <p>Where could this offset coming from? It looks like the children's measure calls are being added to the part, or at least half of it...</p>