0

Ich habe dieses Setup:Android Zentrierung Textview Text nicht vertikal auf dem Gerät/Emulator (aber feinen obwohl in Android Studio Designer-Ansicht)

<RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:id="@+id/postHeader"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="CP" 
      android:id="@+id/initialsView" 
      android:layout_alignTop="@+id/avatarView" 
      android:layout_alignLeft="@+id/avatarView" 
      android:layout_alignBottom="@+id/avatarView" 
      android:layout_alignRight="@+id/avatarView" 
      android:background="@drawable/avatar_background" 
      android:textColor="@color/white" 
      android:gravity="center" 
      android:textStyle="bold" 
      android:textSize="16sp" /> 

     <com.makeramen.roundedimageview.RoundedImageView 
      app:riv_corner_radius="20dp" 
      android:layout_width="40dp" 
      android:layout_height="40dp" 
      android:id="@+id/avatarView" 
      android:layout_alignParentTop="true" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true" 
      android:layout_marginTop="14dp" 
      android:layout_marginLeft="20dp" 
      android:layout_marginBottom="6dp" 
      app:riv_border_color="@color/lightGray" 
      app:riv_border_width="0.2dp" /> 

    ... 
/> 

Die TextView richtet richtig auf die Bildansicht. Alles ist im Android Studio fein:

enter image description here

Allerdings, wenn ich auf den tatsächlichen Gerät ausgeführt (Android 4.4.4) oder Emulator (Android 6.0) bekomme ich den Text ("CP" im Screenshot) stecken alle der Weg nach oben, genauso wie ich keine vertikale Schwerkraft eingestellt habe. Ich habe versucht, textAlignment, Breite und Höhe zu , layout_centerInParent zu both, Schwerkraft zu center_vertical Explicit, includeFontPadding falsch, aber ohne Erfolg. Es zentriert immer noch nicht vertikal (horizontal ist aber in Ordnung).

Was mache ich falsch? Warum wird es in Android Studio trotzdem korrekt angezeigt?

Antwort

0

Okay, ich habe das Problem nach ein wenig Untersuchung herausgefunden. Es scheint, dass es gebrochen wurde: https://code.google.com/p/android/issues/detail?id=63673

die Lösung auf den Link Implementierung mein Problem gelöst:

private static final int AT_MOST_UNSPECIFIED = MeasureSpec.makeMeasureSpec((1<<30)-1, MeasureSpec.AT_MOST); 

    @Override 
    protected void onMeasure(int widthSpec, int heightSpec) 
    { 
    // RelativeLayout has bugs when measured in UNSPECIFIED height mode that were supposedly 
    // fixed in API 18. However the 'fix' does breaks gravity == center_vertical in TextView. 
    // https://code.google.com/p/android/issues/detail?id=63673 
    if (MeasureSpec.getMode(heightSpec) == MeasureSpec.UNSPECIFIED) heightSpec = AT_MOST_UNSPECIFIED; 
    super.onMeasure(widthSpec, heightSpec); 
    }