开发者

RelativeLayout layout_centerInParent and layout_below issue

I have a problem with laying out a Relative Layout. Here is the test code

<RelativeLayout
    android:background="@drawable/bg_controls"
    android:layout_width="fill_parent"
    andro开发者_JS百科id:layout_height="wrap_content">

    <LinearLayout
        android:id="@+id/controls_layout"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/controls_layout" />

</RelativeLayout>

The result of that is

RelativeLayout layout_centerInParent and layout_below issue

If I remove android:layout_centerInParent="true" everything works OK. Why does RelativeLayout fails to do that otherwise?

UPD: the intended result is that a second button appears under the LinearLayout.


I was having a very similar issue, where I had a centered View that was designated layout_centerInParent, and all my other Views were relative to that, but some of the Views were not rendering properly in the editor or on the device.

I found that my centered View itself, designated layout_centerInParent, still had to be relative to something else (as unintuitive as that sounds). So I made it layout_below something that was on top of it, even though that View on top of it was not aligned to anything except being layout_top of the same thing below. But once I did this, the whole UI snapped into place :)


It happens when the relative layout is having height wrap_content. If you can give fixed height to the relative layout, the layout_below the centerInParent item works fine. Otherwise the relative layout assumes the position of the first item (here the controls_layout) in the usual position (that is, without centerInparent) and aligns the second item below it.


solutions:

  1. Give fixed height to relative layout instead of wrap_content or
  2. keep the two buttons in a linear layout with orientation vertical, and give centerInParent to the linear layout


Are you using API level 3 (1.5)? RelativeLayouts are very quirky for 1.5. Giving your buttons an id is a good idea.


Have you tried running this on an actual device? I got this problem when previewing the layout in eclipse, but when I ran it on a device, it worked as intended

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜