【问题标题】:RelativeLayout layout_centerInParent and layout_below issueRelativeLayout layout_centerInParent 和 layout_below 问题
【发布时间】:2016-07-11 15:29:17
【问题描述】:

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

<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 不能做到这一点?

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

【问题讨论】:

  • 你到底想做什么?
  • 预期的结果是第二个按钮出现在 LinearLayout 下。我认为这很明显......
  • 线性布局下,是不是线性布局下?
  • 您能否更具体地介绍一下图片本身而不是布局?
  • RelativeLayout 在这种情况下表现正确,您需要添加 layout_below 参数以获得按钮 2 的预期结果

标签: android android-layout android-relativelayout


【解决方案1】:

我遇到了一个非常相似的问题,我有一个居中的View,它被指定为layout_centerInParent,而我的所有其他视图都与此相关,但一些Views 在编辑器或设备上。

我发现我的居中 View 本身,指定为 layout_centerInParent,仍然必须与其他事物相关(听起来很不直观)。所以我把layout_below 做成了它上面的东西,即使它上面的View 没有与任何东西对齐,除了下面相同的东西layout_top。但是一旦我这样做了,整个用户界面就到位了:)

【讨论】:

    【解决方案2】:

    当相对布局具有高度 wrap_content 时会发生这种情况。如果您可以为相对布局提供固定高度,则 centerInParent 项下的 layout_below 可以正常工作。否则,相对布局假定第一项(此处为 controls_layout)在通常位置(即没有 centerInparent)的位置,并在其下方对齐第二项。


    解决方案:

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          猜你喜欢
          • 2023-04-01
          • 1970-01-01
          • 2011-09-28
          • 2011-03-20
          • 1970-01-01
          • 2011-01-06
          • 2012-10-09
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多