简体   繁体   中英

Android layout_below not working

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.

尝试将android:layout_below="@+id/birth" death_lblandroid:layout_below="@id/birth" ,因为此时它已经被声明, +暗示,它可能会在再次声明时导致问题。

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.

如果修复你的+符号没有帮助,你可以随时将你的id/death定位在id/birth之下,然后将id/death_label置于leftOf id/death

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...

Your problem is that you are calling the properties on the relative layout as if though you are declaring it again.

When you declare an ID for a view then it should be done like this android:id="@+id/button" and when you want Android to position that particular view above or below any other view in a relative layout you have to call it like this android:layout_below="@id/textview"

This tells android that you declared a button with id button and want it to be positioned below the textview, remember do not use @+id to postion view use @id instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM