Android: layout_below not working properly

I have RelativeLayoutwith three views. Both TextViewsshould be to the right of ImageView, and seconds should be below the first TextView.

the 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 is not vertical). enter image description here

Why is this and how can I do the first TextViewvertically in the center?

+4
source share
4 answers

For some reason, the RelativeLayout height parameter sets the problem. Try setting it to 94dp (64 from the image + 15 bottom pad + 15 top pad). This should solve the problem.

+6
source

@f. de . android:layout_height="wrap_content", , , , . match_parent .

+1

TextViews , . padding . , .

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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:paddingBottom="7dp"
    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:paddingTop="7dp"
    android:text="Oxford"
    android:textSize="13sp" />
</RelativeLayout>

, ...

0

Name University RelativeLayout ViewGroup .

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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" />

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="5dp"
        android:layout_marginStart="5dp"
        android:layout_toEndOf="@id/avatar"
        android:layout_toRightOf="@id/avatar">

        <TextView
            android:id="@+id/username"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            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:text="Oxford"
            android:textSize="13sp" />
    </RelativeLayout>
</RelativeLayout>

This makes sense to group related views (TextViews), and also gives a better result, because the entire container is centered, in your original example. The name view is centered vertically, but another view is below it, and this makes it look not how much it will be in the case of the container, where the base level is the main center of the container.

0
source

All Articles