0

I have a Relative Layout with 3 children.

  • 1st is ImageView cover.
  • 2nd is ImageView avatar center in parent and
  • 3rd is TextView app name below avatar and center horizontal.

As in theory it should work but my layout not place text below avatar. What's wrong with RelativeLayout?

P/S: I've tried both android:layout_below="@+id/logo2" and android:layout_below="@id/logo2" but still didn't work!

Thank you!

Here is my xml layout.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:src="@drawable/android_apple"/>

    <ImageView
        android:id="@+id/logo2"
        android:layout_width="64dp"
        android:layout_height="64dp"
        android:layout_centerInParent="true"
        android:src="@drawable/default_avatar"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/logo2"
        android:layout_centerHorizontal="true"
        android:text="AAAAAAA"
        android:textStyle="bold"/>
</RelativeLayout>

And the result:

layout_below not working

phamxuanlu
  • 303
  • 1
  • 5
  • 12

4 Answers4

2

Change the parent Relative Layout height to match parent. It will work.

Like this

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:src="@drawable/android_apple"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
                                          />

    <ImageView
        android:id="@+id/logo2"
        android:layout_width="64dp"
        android:layout_height="64dp"
        android:layout_centerInParent="true"
        android:src="@drawable/default_avatar"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/logo2"
        android:layout_centerHorizontal="true"
        android:text="AAAAAAA"
        android:textStyle="bold"/>
</RelativeLayout>
King of Masses
  • 18,405
  • 4
  • 60
  • 77
-1

try below code

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:src="@drawable/android_apple"/>

    <ImageView
        android:id="@+id/logo"
        android:layout_width="64dp"
        android:layout_height="64dp"
        android:layout_centerInParent="true"
        android:src="@drawable/default_avatar"
        android:layout_margin="5dp"/>

    <RelativeLayout
        android:id="@+id/profile_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/logo"
        android:elevation="4dp"
        android:layout_marginTop="64dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="AAAAAAA"
            android:textStyle="bold"
            android:layout_margin="10dp"
            android:layout_centerHorizontal="true"/>
    </RelativeLayout>
</RelativeLayout>
Omkar
  • 3,040
  • 1
  • 22
  • 42
-1

Try this.

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="10dp"
        android:adjustViewBounds="true"
        android:src="@drawable/android_apple" />

    <ImageView
        android:id="@+id/logo2"
        android:layout_width="64dp"
        android:layout_height="64dp"
        android:layout_centerInParent="true"
        android:src="@drawable/default_avatar" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/logo2"
        android:layout_below="@+id/logo2"
        android:text="AAAAAAA"
        android:textStyle="bold" />

</RelativeLayout>
Diwan Dhanvani
  • 291
  • 1
  • 3
  • 10
-1
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/logo2"
    android:layout_centerHorizontal="true"
    android:text="AAAAAAA"
    android:textStyle="bold"/>

You are using layout_below="@+id/logo2". Use layout_below="@id/logo2", It will work.

K Sathish
  • 247
  • 3
  • 13