Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting minHeight in RelativeLayout, disables alignBaseline?

My RelativeLayout should look more less like this:

+----------------------------------------------+
|                                              |
|                                              |
|+----------------+          +----------------+|
||                |+--------+|                ||
||     View 1     || View 2 ||     View 3     ||
|+----------------++--------++----------------+|
|                                              |
|                                              |
+----------------------------------------------+

and I achieve it perfectly when my relative layout has fixed height (for example 80dp). Unfortunately I need it to have android:layout_height="wrap_content" with android:minHeight set (for example to those 80dp). When I change my layout this way, android:layout_alignBaseline seems not to be working for View2, and I get something similar to this:

+----------------------------------------------+
|                                              |
|                  +--------+                  |
|+----------------+| View 2 |+----------------+|
||                |+--------+|                ||
||     View 1     |          |     View 3     ||
|+----------------+          +----------------+|
|                                              |
|                                              |
+----------------------------------------------+

And my xml looks like this:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="80dp" >

    <TextView
        android:id="@+id/View1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@+id/View2"
        android:gravity="center"
        android:text="View1" />

    <TextView
        android:id="@+id/View2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/View1"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        android:text="View2" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/View2"
        android:gravity="center"
        android:text="View3" />

</RelativeLayout>

Anyone got any idea why it doesn't work?

like image 978
Bartek Lipinski Avatar asked Nov 01 '22 18:11

Bartek Lipinski


1 Answers

After few tests I came to the conclusion that setting height to wrap_content in RelativeLayout is just bugged. It cannot cooperate correctly with views that does not fill the whole height of the layout. For this kind of views RelativeLayout has a problem with centering vertically (rule CENTER_VERTICAL or CENTER IN PARENT). Basically, views that should be centered vertically WILL BE DISPLAYED like they are, but the layout itself will "SEE" them (for the purpose of setting other rules) as aligned top.

Therefore setting any rules as dependent on the position of those "wrongly-perceived-by-the-layout" views, will result in views that are being displayed in an unintended way.

If you take this into account, my case was pretty simple to explain. This is how the views were displayed:

+----------------------------------------------+
|                                              |
|                  +--------+                  |
|+----------------+| View 2 |+----------------+|
||                |+--------+|                ||
||     View 1     |          |     View 3     ||
|+----------------+          +----------------+|
|                                              |
|                                              |
+----------------------------------------------+

And this is how RelativeLayout "perceived" them:

+----------------------------------------------+
|+----------------+          +----------------+|
||                |+--------+|                ||
||     View 1     || View 2 ||     View 3     ||
|+----------------++--------++----------------+|
|                                              |
|                                              |
|                                              |
|                                              |
+----------------------------------------------+

(the View 2 position is the same in both pictures).

Since I needed my layout to be RelativeLayout, to solve this issue I decided to give up completely on using wrap_content value, and control the height of the view myself. It complicates my layout a bit, but as long as I control it, its fine.

like image 129
Bartek Lipinski Avatar answered Nov 15 '22 04:11

Bartek Lipinski