简体   繁体   English

Android layout_below无效

[英]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. 我在我的应用程序中广泛使用RelativeLayouts,并认为我知道如何指定它们,但这让我感到茫然。 I am basically positioning 4 TextViews in two rows of two each consisting of a label and text that will be supplied. 我基本上将4个TextView放在两行,每行两行,每行包含一个标签和将要提供的文本。 It should look something like: 它应该看起来像:

Born: 23 Aug 1810 Mason Co., Kentucky 出生:1810年8月23日,肯塔基州梅森公司

Died: 15 Jul 1865 Cincinnati, Hamilton Co., Ohio 死了:1865年7月15日辛辛那提,俄亥俄州汉密尔顿公司

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! 如果我将death_lbl视图的规格更改为'layout_below =“@ + id / birth_lbl”',则行正确定位! 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". 但是,“出生”视图可以换行到多行,所以我真的需要将第二行放在“出生”下面,而不是“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. 它出现在Eclipse的Graphical Layout编辑器和运行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 . 我也遇到了同样的问题,因为我使用的是约束布局,但align_below仅适用于相对布局 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). 我实际上能够通过仅使用那些字段编写临时layout来复制这种现象,并且如果我没有将初始birth_lbl视图相对birth_lbl视图定位,则能够确定问题消失了(“given_layout”在此案件)。

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 . 所以我不确定这是否被归类为修复,变通方法或者kludge(现在仍然是一个词?),但我所做的是将文本视图放在他们自己的RelativeLayout并定位RelativeLayout相对到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" 当您为视图声明ID时,它应该像这个android:id="@+id/button"并且当您希望Android将该特定视图放置在相对布局中的任何其他视图的上方或下方时,您必须调用它喜欢这个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. 这告诉android你声明了一个带有id按钮的按钮并希望它位于textview下面,记住不要使用@+id来定位视图使用@id代替。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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