I have a RelativeLayout inside a CardView, with a ImageView and two TextViews. One TextView has to be centered, so it is placed over the ImageView. The other TextView has to be below that TextView, but for some reason unknown to me it places over the first TextView. I managed to set it properly by adding a second RelativeLayout and putting the two TextViews inside it, and now they show properly. But I am curious about why if you don't add that second RelativeLayout, the second TextView doesn't appears below the first one. Someone can explain me? Thank you.
The layout:
This way works:
<?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"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:elevation="10dp"
app:cardUseCompatPadding="true"
android:id="@+id/cv">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/image"
android:layout_alignParentTop="true"
android:adjustViewBounds="true"
android:src="@drawable/ic_launcher"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/name"
android:gravity="center"
android:textStyle="bold"
android:textSize="40sp"
android:shadowColor="#ffffff"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="6"
android:textColor="@color/black"
android:text="I am the name"
android:layout_centerInParent="true" />
<TextView
android:id="@+id/type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/name"
android:gravity="center"
android:layout_centerHorizontal="true"
android:shadowColor="#ffffff"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="6"
android:text="I am the type"
android:textColor="@color/red"
android:textSize="25sp"
android:textStyle="bold" />
</RelativeLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
This doesn't:
<?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"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:elevation="10dp"
app:cardUseCompatPadding="true"
android:id="@+id/cv">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/image"
android:layout_alignParentTop="true"
android:adjustViewBounds="true"
android:src="@drawable/ic_launcher"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/name"
android:gravity="center"
android:textStyle="bold"
android:textSize="40sp"
android:shadowColor="#ffffff"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="6"
android:textColor="@color/black"
android:text="I am the name"
android:layout_centerInParent="true" />
<TextView
android:id="@+id/type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/name"
android:gravity="center"
android:layout_centerHorizontal="true"
android:shadowColor="#ffffff"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="6"
android:text="I am the type"
android:textColor="@color/red"
android:textSize="25sp"
android:textStyle="bold" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>