我在布局相对布局时遇到了问题。以下是测试代码
<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>
其结果是

如果我删除android:layout_centerInParent="true“,一切工作正常。为什么RelativeLayout不能做到这一点呢?
更新:预期的结果是在LinearLayout下出现第二个按钮。
发布于 2014-09-06 14:41:55
我遇到了一个非常类似的问题,我有一个居中的View,它被指定为layout_centerInParent,我所有的其他视图都是相对于它的,但是一些View不能在编辑器中或在设备上正确呈现。
我发现我的中心View本身,指定为layout_centerInParent,仍然必须相对于其他东西(尽管这听起来很不直观)。所以我把它设为layout_below上面的某个东西,尽管它上面的View除了是下面同一个东西的layout_top之外,并不与任何东西对齐。但一旦我这样做了,整个UI就会很快就位:)
发布于 2016-07-11 15:29:17
当相对布局的高度为wrap_content时,就会发生这种情况。如果您可以为相对布局提供固定的高度,那么layout_below centerInParent项就可以正常工作。否则,相对布局假定第一个项目(这里是controls_layout)的位置在通常位置(即没有centerInparent),并将第二个项目对齐到它下面。
解决方案:
发布于 2011-05-17 03:59:42
您是否正在使用API level 3 (1.5)?RelativeLayouts对于1.5来说是非常怪异的。给你的按钮一个id是个好主意。
https://stackoverflow.com/questions/6022553
复制相似问题