Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android:layout_below not properly working

Tags:

android

I have a RelativeLayout with three views. Both TextViews should be to right of the ImageView and the seconds one should be below the first TextView.

Code:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#bdbdbd"
    android:paddingBottom="15dp"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:paddingTop="15dp" >

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

    <TextView
        android:id="@+id/username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@id/avatar"
        android:text="Chris"
        android:textSize="16sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/university"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/username"
        android:layout_toRightOf="@id/avatar"
        android:text="Oxford"
        android:textSize="13sp" />
</RelativeLayout>

Result: enter image description here

After removing android:layout_centerVertical="true" from the first TextView, everything works as expected (except that it's not vertical). enter image description here

Why is this and how can I make the first TextView vertical centered?

like image 966
Chris Avatar asked Dec 12 '14 17:12

Chris


2 Answers

For some reason the RelativeLayout's height param is giving the problem. Try setting it to 94dp (64 of the image + 15 of bottom padding + 15 of top padding). This should solve the problem

like image 53
Francesco D.M. Avatar answered Sep 22 '22 17:09

Francesco D.M.


@f. de is right. The problem is android:layout_height="wrap_content", as the height of relative layout is determined my it's content setting it's content to vertically center based on it's height wont work. You need to set this to match_parent or to a fixed value.

like image 38
vipul mittal Avatar answered Sep 21 '22 17:09

vipul mittal