Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android layout_below not working

Tags:

android

I use RelativeLayouts extensively in my app and thought I knew how to specify them, but this has me at a loss. I am basically positioning 4 TextViews in two rows of two each consisting of a label and text that will be supplied. It should look something like:

Born: 23 Aug 1810 Mason Co., Kentucky

Died: 15 Jul 1865 Cincinnati, Hamilton Co., Ohio

This is the relevant portion of the layout:

        <TextView android:id="@+id/birth_lbl"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/given_layout"
            android:layout_alignLeft="@+id/given_layout"
            android:layout_marginTop="6dip"
            style="@style/label"
            android:text="@string/born"
        />
        <TextView android:id="@+id/birth"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/birth_lbl"
            android:layout_alignBaseline="@+id/birth_lbl"
            android:layout_marginLeft="10dip"
            android:layout_marginRight="6dip"
            style="@style/main_text"
            android:text="dd Mmm yy"
        />
        <TextView android:id="@+id/death_lbl"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/birth"
            android:layout_alignLeft="@+id/birth_lbl"
            android:layout_marginTop="4dip"
            style="@style/label"
            android:text="@string/died"
        />
        <TextView android:id="@+id/death"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/death_lbl"
            android:layout_alignLeft="@+id/birth"
            android:layout_alignBaseline="@+id/death_lbl"
            android:layout_marginRight="6dip"
            style="@style/main_text"
            android:text="dd Mmm yy"
        />

For some reason, this displays the death line views ABOVE the birth line views! If I change the spec of the death_lbl view to instead be 'layout_below="@+id/birth_lbl"', the lines are positioned correctly! However, it is possible for the "birth" view to wrap to multiple lines, so I really need to position the 2nd line below "birth", not "birth_lbl".

Anyone know the reason for this behavior? It occurs both in the Graphical Layout editor in Eclipse and at runtime on my tablet running Android 4.0.

like image 315
gordonwd Avatar asked Feb 19 '12 18:02

gordonwd


4 Answers

Try changing android:layout_below="@+id/birth" in death_lbl to android:layout_below="@id/birth", because at this point it is already declared, which the + implies, it could lead to problems when declaring it again.

like image 167
Demonick Avatar answered Nov 04 '22 08:11

Demonick


I was also facing the same problem because I was using constraint layout, but align_below works only in Relative Layout. so check which layout you are using.

like image 43
Deepak Gautam Avatar answered Nov 04 '22 09:11

Deepak Gautam


If fixing your + signs doesn't help, you could always position your id/death below id/birth, and then put id/death_label toLeftOf id/death.

like image 1
dokkaebi Avatar answered Nov 04 '22 10:11

dokkaebi


I was actually able to duplicate this phenomenon by coding up a temporary layout with only those fields, and was able to determine that the problem went away if I did not position the initial birth_lbl view relative to the view above it ("given_layout" in this case).

So I'm not sure if this is classified as a fix, a workaround, or a kludge (is that still a word these days?), but what I did was to position the text views inside their own RelativeLayout and position the RelativeLayout relative to id/given_layout. In any case, it works...

like image 1
gordonwd Avatar answered Nov 04 '22 08:11

gordonwd