Android Relativelayout align items

Asked

Viewed 1,770 times

0

I am not being able to align vertically in the center the items of the third line marked in the image. This is a layout of items in a listview.

I’ve tried Gravity="center", layout_gravity="center", layout_centerVertical="true".

I would not like to use a Linearlayout to group the items, some idea ?

inserir a descrição da imagem aqui

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:padding="5dp">

    <TextView
        style="@style/textBase"
        android:id="@+id/labNom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Nome da categoria"/>

    <TextView
        style="@style/textNote"
        android:id="@+id/labDes"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/labNom"
        android:paddingTop="8dp"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:text="Descrição longa da categoria"/>

    <ImageView
        android:id="@+id/img1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/labDes"
        android:layout_alignParentRight="true"
        android:src="@drawable/ic_thumb_up"/>

    <TextView
        android:id="@+id/lab1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/labDes"
        android:layout_toLeftOf="@+id/img1"
        android:text="57"/>

</RelativeLayout>
  • What is the size of the image (mdpi)?

  • 32x32. But the emulator is using 36x36 (Nexus S hdpi). Ooops! could be this.

  • I fixed the resolution of the images, 24x24 - 36x36 - 48x48 and 72x72. No result.

  • The image can be larger than the text if you can’t align it without using padding or margin. Try this: Remove android:layout_below="@id/labDes" of the last textView and add android:layout_alignBaseline="@+id/img1". The image and text should be aligned below, if the centers do not align it is because they have different heights.

1 answer

1

Puts his ImageView and the TextView within a LinearLayout with gravity=center and it’s all right, this way:

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/labDes"
        android:layout_alignParentRight="true"
        android:gravity="center">

        <TextView
            android:id="@+id/lab1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"    
            android:layout_toLeftOf="@+id/img1"
            android:text="57"
            />
        <ImageView
            android:id="@+id/img1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/labDes"
            android:src="@drawable/ic_thumb_up"
            />
    </LinearLayout>

Good Luck!

  • As quoted in the question I would not like to use a Linearlayout to group the items. Anyway thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.