Error in Android RelativeLayout when wrapping_content

83 views Asked by At

I have a relative Layout with 2 Views. An Imageview and a Textview. The textview should have the same width as the imageview and be below of the imageview.

So I set them like this

 <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:maxWidth="50dp"
        android:minWidth="50dp"
        android:layout_centerVertical="true"
        android:layout_toRightOf=some_other_view
        android:layout_marginLeft="8dp"
        android:minHeight="10dp" />

 <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/imageView"
        android:layout_below="@+id/imageView"
        android:singleLine="true"
        android:text="stuff"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="@android:color/white"
        android:textSize="12dp"
        android:textStyle="bold"
        android:layout_marginTop="4dp"
        android:layout_alignRight="@+id/imageView"
        android:gravity="center" />

This works fine as long as the relativLayout is set to fill_parent. But when I try to wrap_content the height of the relativLayout, the Textview jumps on the top of the imageView... I don't get my mistake. Can anyone help me?

1

There are 1 answers

0
Nikunj Dobariya On

you just need to give the layout_gravity="center_vertical" property to the Relative Layout...
try this solution

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_vertical"
    android:orientation="vertical" >


 <ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView"
    android:maxWidth="50dp"
    android:minWidth="50dp"
    android:layout_centerVertical="true"
    android:layout_toRightOf=some_other_view
    android:layout_marginLeft="8dp"
    android:minHeight="10dp" />

 <TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/imageView"
    android:layout_below="@+id/imageView"
    android:singleLine="true"
    android:text="stuff"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textColor="@android:color/white"
    android:textSize="12dp"
    android:textStyle="bold"
    android:layout_marginTop="4dp"
    android:layout_alignRight="@+id/imageView"
    android:gravity="center" />

</RelativeLayout>