I have a layout that looks roughly like this:
<RelativeLayout>
<TextView
layout_width="match_parent"
layout_height="50dp"
layout_alignParentTop="true"
id="@+/A"/>
<TextView
layout_width="match_parent"
layout_height="46dp"
layout_alignParentBottom="true"
id="@+/B"/>
<CustomView
layout_width="wrap_content"
layout_height="wrap_content"
layout_below="@id/A"
layout_above="@id/B"
layout_centerHorizontal="true"/>
</RelativeLayout>
The intention is that A and B should go at the top and bottom with fixed heights, and CustomView should take as much of the remaining space as it can subject to a constraint on its aspect ratio. (Note I can't use LinearLayout because of some other children I have omitted)
Here's what happens for a particular screen size:
- CustomView.onMeasure is called with (AT_MOST 776, AT_MOST 395)
- CustomView calls setMeasuredDimension(285, 391)
- CustomView.onMeasure called again with (EXACTLY 285, EXACTLY 251)
- CustomView calls setMeasuredDimension (arguments don't seem to matter)
- CustomView.onLayout called with dimensions 285 x 251
I guess the top and bottom constraints force height to be 251, but why doesn't RelativeLayout give CustomView a chance to choose a width based on this height constraint? It seems incorrect behaviour of RelativeLayout to assume that if a child can be 285x391, then it can also be 285x251. Is there a workaround (besides replacing RelativeLayout by another custom view)?
I'm using minSdkVersion=14 and targetSdkVersion=21, in case it matters.