4

I have a probleme with the SDK 18. The gravity of my text view doesn't work vertically. But it's working with sdk17.

Pictures and code :

header_of_listView.xml :

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/RelativeLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/iv_bg"
            android:layout_width="wrap_content"
            android:layout_height="@dimen/entete.size.h"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_centerHorizontal="true"
            android:contentDescription="@string/blank"
            android:scaleType="fitXY"
            android:src="@drawable/cellule_flecher_mise_en_valeur" />

        <TextView
            android:id="@+id/tv_message"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@id/iv_bg"
            android:layout_alignLeft="@id/iv_bg"
            android:layout_alignRight="@id/iv_bg"
            android:layout_alignTop="@id/iv_bg"
            android:layout_marginBottom="6dp"
            android:gravity="center"
            android:text="@string/a_remplacer"
            android:textColor="@color/tableviewText"
            android:textSize="18sp"
            android:textStyle="bold" 
            android:layout_centerHorizontal="true"/>

    </RelativeLayout>

AndroidManifest.xml

<uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

sdk = 17

AndroidManifest.xml

<uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

sdk = 18

with black background for textview android:background="#000000"

sdk 18 with black background

If someone have an idea. I have try to change wrap_content with match_parent.

The only thing that work is android:layout_height="@dimen/entete.size.h" on the TextView, but the margin doesn't apply. For the moment I leave the project to SDK 17.

WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
Anthone
  • 2,156
  • 21
  • 26

3 Answers3

9

The problem is the RelativLayout as root layout of the listview item. If your put a LinearLayout around the RelativeLayout it works also with targetSdk = 19. Same problem appears with the GridView.

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

   ... your layout like before

</LinearLayout>

This is not really a nice workaround - but better than put targetSdk = 17 I guess.

Informatic0re
  • 6,300
  • 5
  • 41
  • 56
  • I had the same problem as the original poster but with a TextView inside a CardView inside a RecyclerView. Wrapping the entire CardView layout in a match_parent LinearLayout fixed it. Thank you so much! – Flyview Oct 22 '16 at 03:57
  • I was wrong, that didn't fix it for me. The cards started being as wide as the parent, which isn't what I wanted. I had to wrap one of the RelativeLayouts inside the CardView element, in a useless LinearLayout. – Flyview Oct 22 '16 at 16:39
0

I had the same problem and it happened only in Android 19. I target api 23 and it works fine.

I found out that giving a height to the textview fixed the problem

It was like this:

         <TextView
            android:id="@+id/tv_message"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@id/iv_bg"
            android:layout_alignLeft="@id/iv_bg"
            android:layout_alignRight="@id/iv_bg"
            android:layout_alignTop="@id/iv_bg"
            android:layout_marginBottom="6dp"
            android:gravity="center"
            android:text="@string/a_remplacer"
            android:textColor="@color/tableviewText"
            android:textSize="18sp"
            android:textStyle="bold" 
            android:layout_centerHorizontal="true"/>

I made it like this:

         <TextView
            android:id="@+id/tv_message"
            android:layout_width="wrap_content"
            android:layout_height="40dp"            <!-- this is the key-->      
            android:layout_alignBottom="@id/iv_bg"
            android:layout_alignLeft="@id/iv_bg"
            android:layout_alignRight="@id/iv_bg"
            android:layout_alignTop="@id/iv_bg"
            android:layout_marginBottom="6dp"
            android:gravity="center"
            android:text="@string/a_remplacer"
            android:textColor="@color/tableviewText"
            android:textSize="18sp"
            android:textStyle="bold" 
            android:layout_centerHorizontal="true"/>

I didn't want to add another linear layout because of performance issues.

Ultimo_m
  • 4,724
  • 4
  • 38
  • 60
-4

remove android:targetSdkVersion="18" and run.

Looking Forward
  • 3,579
  • 8
  • 45
  • 65