【问题标题】:RelativeLayout children not aligning to a centered ImageViewRelativeLayout 子级未与居中的 ImageView 对齐
【发布时间】:2015-09-02 06:31:20
【问题描述】:

有点尴尬,但我在将一些视图对齐到 RelativeLayout 时遇到了一些基本问题。我的 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),我想要两个视图在下方对齐 - 一个在左侧,一个在右侧。但是,对于上面的 xml,似乎两个较小的视图正在与 id/imageview 对齐,而没有考虑到它已经居中的事实:



在我将 id/imageview 居中之前,它看起来如下所示(请注意,较小的视图现在按预期对齐,尽管它们没有移动):



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

【问题讨论】:

    标签: android android-layout alignment android-relativelayout


    【解决方案1】:

    您是否有理由希望您的 RelativeLayout layout_width 和高度来包装内容?将它们设置为 match_parent 可以帮助您进行中心对齐。

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

    【讨论】:

    • 更改为 match_parent 已修复 - 谢谢!不知道为什么会这样。
    【解决方案2】:

    将 align_left 替换为 to_left 并将 align_right 替换为 to_right

    【讨论】:

    • 谢谢,但这不是我要找的。 “align_left”和“align_right”是我正在寻找的行为,当对齐的视图居中时它似乎没有更新。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-07
    • 2014-07-11
    • 2020-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-20
    相关资源
    最近更新 更多