有点尴尬,但我在对齐 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/