2011-05-16 102 views
2

我在布局相对布局时遇到了问题。下面是测试代码 RelativeLayout layout_centerInParent和layout_below问题

<RelativeLayout 
    android:background="@drawable/bg_controls" 
    android:layout_width="fill_parent" 
    android: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> 

的,其结果是enter image description here

如果我删除的android:layout_centerInParent = “真” 一切正常确定。为什么RelativeLayout无法做到这一点呢?

UPD:预期结果是第二个按钮出现在LinearLayout下。

+0

预期结果是什么? – prolink007 2011-05-16 19:54:14

+0

你究竟在做什么? – FoamyGuy 2011-05-16 19:54:58

+0

预期的结果是第二个按钮出现在LinearLayout下。我认为这是显而易见的... – 2011-05-16 19:56:59

回答

0

您使用API​​级别3(1.5)吗? RelativeLayouts对于1.5非常古怪。给你的按钮一个ID是个好主意。

+0

我使用1.6 10个字符 – 2011-05-16 20:01:27

0

您是否尝试过在实际设备上运行此操作?我在eclipse中预览布局时遇到了这个问题,但是当我在设备上运行它时,它按预期工作

1

我遇到了一个非常类似的问题,其中有一个中心的View,它被指定为layout_centerInParent,其他视图与此相关,但某些View在编辑器或设备上没有正确渲染。

我发现我的中心View本身,指定layout_centerInParent,仍然必须是相对于别的东西(因为听起来不直观)。所以我在layout_below之上做了一些东西,尽管它上面的View没有对齐,除了下面是同一事物的layout_top。但是,一旦我这样做,整个用户界面捕捉到:)

1

它发生在相对布局有高度wrap_content时。如果您可以给定相对布局的固定高度,那么centerInParent项目的layout_below工作正常。否则,相对布局将假定第一个项目(这里是controls_layout)在通常位置(即没有centerInparent)的位置,并将其下面的第二个项目对齐。


解决方案:

  1. 给固定的高度相对于布局而不是WRAP_CONTENT 或
  2. 保持两个按钮与方向垂直的线性布局,并给予centerInParent到线性布局