android - RelativeLayout 子级未与居中的 ImageView 对齐

标签 android android-layout alignment android-relativelayout

有点尴尬,但我在对齐 RelativeLayout 中的某些 View 时遇到了一些基本问题。我的 XML 如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context=".MyActivity">

<ImageView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/background"
    android:scaleType="centerCrop"
    android:src="@drawable/background"/>

<ImageView
    android:layout_width="300dp"
    android:layout_height="300dp"
    android:layout_centerInParent="true"
    android:id="@+id/imageview"
    android:src="@drawable/text" />

<Button
    android:layout_width="200dp"
    android:layout_height="60dp"
    android:enabled="false"
    android:id="@+id/button"
    android:background="@drawable/buttonxml"
    android:layout_below="@+id/imageview"
    android:layout_alignLeft="@id/imageview"/>

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/coin"
    android:src="@drawable/coin"
    android:layout_below="@id/imageview"
    android:layout_alignRight="@id/imageview"/>

</RelativeLayout>

我有一个居中的 ImageView (id/imageview),我想要两个 View 在下方对齐 - 一个在左边,一个在右边。但是,对于上面的 xml,似乎两个较小的 View 正在与 id/imageview 对齐,而没有考虑它已居中的事实:



在我将 id/imageview 居中之前,它看起来像下面这样(请注意,较小的 View 现在已按预期对齐,尽管它们没有移动):



除了使用 LinearLayouts 的集合之外还有什么想法吗?我觉得这对于使用 RelativeLayout 处理的案例来说足够简单,我可能只是在做一些愚蠢的事情。提前致谢!

最佳答案

您是否有理由希望您的 RelativeLayout layout_width 和高度来包裹内容?将它们设置为 match_parent 可能有助于居中对齐。

顺便说一下,您的Button 设置android:layout_below="@+id/imageview" 而不是android:layout_below="@id/imageview"

关于android - RelativeLayout 子级未与居中的 ImageView 对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32341420/

相关文章:

android - 在 RecyclerView 中删除最后一项时应用程序崩溃

安卓风格说明

android - 元素与共享元素转换重叠状态和操作栏

java - 类转换异常 : fragment 1 cannot be cast to fragment 2

android - 何时使用 9-Patch

java - 可观察到按需发出项目

java - ListView 未对齐

HTML:表格后的段落,出现在表格的右侧

html - 如何使用 CSS 为所有浏览器垂直居中 div 元素?

css - 保持文本顶部对齐,图像与相对高度 div 的底部对齐