4

I have a simple list item layout to show a image and a title.

<RelativeLayout            
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/image"
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:src="@drawable/image" />

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignTop="@+id/image"
            android:layout_alignBottom="@+id/image"
            android:layout_toRightOf="@+id/image"
            android:gravity="center_vertical"
            android:text="Title" />
</RelativeLayout>

My problem is the text gravity which should be "center-vertical". When I display the view bounds in the developer setting then I can see the TextView size is greater then the text itself but the gravity stays on top. The problem appears on my android KitKat device but not on Android Froyo for example.

Onik
  • 19,396
  • 14
  • 68
  • 91
Happo
  • 1,375
  • 3
  • 16
  • 34

4 Answers4

2

This is a known issue that has already been fixed and it doesn't affect Marshmallow (I haven't tried it on Lollipop though). The (not so elegant) solution is to wrap your TextView in a FrameLayout:

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

  <ImageView
    android:id="@+id/image"
    android:layout_width="48dp"
    android:layout_height="48dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:src="@drawable/image" />

  <FrameLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@id/image"
    android:layout_alignBottom="@id/image"
    android:layout_toRightOf="@id/image"
    android:layout_centerInParent="true">

    <TextView
      android:id="@+id/title"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center_vertical"
      android:text="Title" />

  </FrameLayout>

</RelativeLayout>
Flávio Faria
  • 6,575
  • 3
  • 39
  • 59
0

Change android:gravity="center_vertical" to android:layout_centerInParent="true"

Simply use this layout,it will work

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

            <ImageView
                android:id="@+id/image"
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:src="@drawable/image" />

            <TextView
                android:id="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_toRightOf="@+id/image"
                android:layout_centerInParent="true"
                android:text="Title" />
</RelativeLayout>
Giru Bhai
  • 14,370
  • 5
  • 46
  • 74
0
// Try this way,hope this will help you to solve your problem.

<LinearLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:gravity="center_vertical">

   <ImageView
    android:id="@+id/image"
    android:layout_width="48dp"
    android:layout_height="48dp"
    android:src="@drawable/ic_launcher" />

   <TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Title" />

</LinearLayout>
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67
  • Thank you for the help but i want to archive it with a RelativeLayout because i broke down my problem to the simplest one but i a have more then 8 child views in the layout. – Happo Jun 11 '14 at 12:18
0

Check this layout code and

image

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

            <ImageView
                android:id="@+id/image"
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:src="@drawable/ic_launcher" />

            <TextView
                android:id="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"

                android:layout_toRightOf="@+id/image"
                android:layout_centerInParent="true"
                android:text="@string/hello_world" />
            </RelativeLayout>
Aniruddha
  • 4,477
  • 2
  • 21
  • 39