RelativeLayout children not aligning to a centered ImageView

时间:2015-09-01 21:47:00

标签: android android-layout alignment android-relativelayout

Kind of embarrassing, but I'm having some basic issues aligning some views in a RelativeLayout. My XML is Below:

<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>

I have a centered ImageView (id/imageview) and I want two views aligned below - one to the left and one to the right. However, with the xml above, it seems that the two smaller views are being aligned to id/imageview without taking into account the fact that it has been centered:



Before I center id/imageview, it looks like the following (note that the smaller views are now aligned as expected though they haven't moved):



Any ideas here other than using a collection of LinearLayouts? I feel like this is simple enough of a case to be handled with a RelativeLayout and I am probably just doing something stupid. Thanks in advance!

2 个答案:

答案 0 :(得分:1)

Is there a reason you want your RelativeLayout layout_width and height to wrap content? Setting them to match_parent may help you with center alignment.

By the way, your Button sets android:layout_below="@+id/imageview" instead of android:layout_below="@id/imageview".

答案 1 :(得分:0)

Replace align_left by to_left and align_right by to_right