0

I want to set several images of varying sizes to an ImageView one after another. Each image should be positioned at the bottom center of the ImageView, but I cannot get it to work. The images are centered, but not at the bottom. Here's my XML, which I based on this thread, the ImageView in question is at the end (animal). How can I position the image in the center AND bottom of the ImageView?

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:soundEffectsEnabled="true"
    tools:context=".GameActivity">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/level_background"
        android:scaleType="fitXY"
        android:adjustViewBounds="true"
        android:contentDescription="TODO" />

    <ImageButton
        android:layout_width="120dp"
        android:layout_height="80dp"
        android:layout_margin="30dp"
        android:src="@drawable/button_back"
        android:scaleType="fitStart"
        android:background="@null"
        android:id="@+id/button_back"
        android:contentDescription="@string/button_back"
        android:onClick="onClick" />

    <RelativeLayout
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:layout_marginTop="30dp"
        android:layout_gravity="center_horizontal"
        android:scaleType="fitCenter"
        android:id="@+id/image_progress"
        android:background="@null"
        android:orientation="horizontal" />

    <ImageButton
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:layout_marginTop="30dp"
        android:layout_gravity="center_horizontal"
        android:scaleType="fitCenter"
        android:background="@null"
        android:id="@+id/button_letter"
        android:contentDescription="@string/button_letter"
        android:onClick="onClick"/>

    <ImageButton
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:layout_marginTop="680dp"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/button_speaker"
        android:scaleType="fitCenter"
        android:background="@null"
        android:id="@+id/button_animal"
        android:contentDescription="@string/button_animal"
        android:onClick="onClick"/>

    <LinearLayout
        android:id="@+id/left"
        android:layout_width="380dp"
        android:layout_height="fill_parent"
        android:layout_gravity="start"
        android:orientation="vertical />

    <LinearLayout
        android:id="@+id/right"
        android:layout_width="380dp"
        android:layout_height="match_parent"
        android:layout_gravity="end"
        android:orientation="vertical"/>

    <LinearLayout
        android:id="@+id/middle"
        android:layout_width="380dp"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        android:orientation="vertical />

    <RelativeLayout
        android:id="@+id/rl"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal|bottom">

        <ImageView
            android:id="@+id/animal"
            android:layout_height="300dp"
            android:layout_width="300dp"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_gravity="bottom"
            android:layout_marginTop="400dp"
            android:contentDescription="@string/animal"/>

    </RelativeLayout>
</FrameLayout>
twinrix
  • 43
  • 7

1 Answers1

1

Your bottom imageview has layout_width set to 0dp. You should make it match_parent or wrap_content

<RelativeLayout
        android:id="@+id/rl"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal|bottom">

        <ImageView
            android:id="@+id/animal"
            android:layout_height="300dp"
            android:layout_width="300dp"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_gravity="bottom"
            android:layout_marginTop="400dp"
            android:contentDescription="@string/animal"/>

    </RelativeLayout>
bijaykumarpun
  • 677
  • 4
  • 9
  • That didn't help unfortunately. I ended up using the matrix scaletype and scaling and translating the image myself. – twinrix Dec 08 '19 at 10:21