首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RelativeLayout layout_centerInParent和layout_below问题

RelativeLayout layout_centerInParent和layout_below问题
EN

Stack Overflow用户
提问于 2011-05-17 03:51:28
回答 4查看 3.3K关注 0票数 2

我在布局相对布局时遇到了问题。以下是测试代码

代码语言:javascript
复制
<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下出现第二个按钮。

EN

回答 4

Stack Overflow用户

发布于 2014-09-06 14:41:55

我遇到了一个非常类似的问题,我有一个居中的View,它被指定为layout_centerInParent,我所有的其他视图都是相对于它的,但是一些View不能在编辑器中或在设备上正确呈现。

我发现我的中心View本身,指定为layout_centerInParent,仍然必须相对于其他东西(尽管这听起来很不直观)。所以我把它设为layout_below上面的某个东西,尽管它上面的View除了是下面同一个东西的layout_top之外,并不与任何东西对齐。但一旦我这样做了,整个UI就会很快就位:)

票数 1
EN

Stack Overflow用户

发布于 2016-07-11 15:29:17

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

解决方案:

  1. 将固定高度赋予相对布局,而不是wrap_content或
  2. 将这两个按钮保持在垂直方向的线性布局中,并将centerInParent赋予线性布局
票数 1
EN

Stack Overflow用户

发布于 2011-05-17 03:59:42

您是否正在使用API level 3 (1.5)?RelativeLayouts对于1.5来说是非常怪异的。给你的按钮一个id是个好主意。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6022553

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档