RelativeLayout 子级未与居中的 ImageView 对齐
RelativeLayout children not aligning to a centered ImageView
有点尴尬,但我在对齐 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 处理的案例来说足够简单,我可能只是在做一些愚蠢的事情。提前致谢!
将 align_left 替换为 to_left,将 align_right 替换为 to_right
您是否希望 RelativeLayout layout_width
和高度包裹内容?将它们设置为 match_parent
可能有助于您居中对齐。
顺便说一下,您的 Button
设置 android:layout_below="@+id/imageview"
而不是 android:layout_below="@id/imageview"
。
有点尴尬,但我在对齐 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 处理的案例来说足够简单,我可能只是在做一些愚蠢的事情。提前致谢!
将 align_left 替换为 to_left,将 align_right 替换为 to_right
您是否希望 RelativeLayout layout_width
和高度包裹内容?将它们设置为 match_parent
可能有助于您居中对齐。
顺便说一下,您的 Button
设置 android:layout_below="@+id/imageview"
而不是 android:layout_below="@id/imageview"
。