RelativeLayout and TextView with broken gravity

I have a simple list item layout to show image and 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 gravity of the text, which should be "center-vertical". When I show the boundaries of the view in the developer’s settings, I see that the size is TextViewlarger than the text itself, but gravity remains on top. The problem appears on my Android device KitKat, but not on Android Froyo, for example.

+4
source share
4 answers

, ​​ Marshmallow ( Lollipop), ( ) , TextView 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>
+2

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

,

<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>
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>
0
source

Check out 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>
0
source

Source: https://habr.com/ru/post/1544148/


All Articles