-1

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.

stewbasic
  • 831
  • 7
  • 21

2 Answers2

0

I wrote code like this

`

<RelativeLayout
    android:layout_width="wrap_content"
    android:background="@color/material_blue_grey_800"
    android:layout_height="wrap_content">

    <TextView android:id="@+id/A"
        android:layout_width="match_parent"
        android:layout_alignParentTop="true"
        android:text="TOP TEXT"
        android:textColor="#ff0000"
        android:layout_height="50dp" />

    <TextView android:id="@+id/B"
        android:layout_width="match_parent"
        android:layout_alignParentBottom="true"
        android:text="Bottom text"
        android:textColor="#00ff00"
        android:background="#820f22"
        android:layout_height="50dp" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/A"
        android:layout_above="@+id/B"
        android:background="#0ff0ff"
        android:src="@drawable/abc_btn_check_material"
        android:layout_centerInParent="true"/>


</RelativeLayout>

`

and you can check it work good for me, you can try to change your custom view height to "match_parent" and remove gravity orientation

Hayk Petrosyan
  • 363
  • 1
  • 6
  • I tried with all combinations of wrap_content/match_parent for height and centerInParent/centerHorizontal/no gravity. For the height constrained case, all produce this: http://i.imgur.com/vrE0qxa.png. Note the aqua background indicates the ImageView is not square, though the image is centered inside the ImageView. Maybe I can't assume that CustomView can choose its aspect ratio. – stewbasic May 03 '16 at 09:36
-1

just put weight and height to match parent to center view and also remove layout_above as you can find in my code.

<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="match_parent"
    layout_below="@id/A"
    weight=1
    layout_centerHorizontal="true"/>
</RelativeLayout>

The reason behind this code work is your top and bottom view has fix value so layout has to make space for it even center view has weightage with 1. sorry for typo.

KDeogharkar
  • 10,939
  • 7
  • 51
  • 95
  • layout_weight has no effect in a RelativeLayout http://stackoverflow.com/a/31345745/1710519 Removing layout_above causes the child to stretch to the bottom http://i.imgur.com/yRZaYpo.png – stewbasic May 03 '16 at 09:26