There is an xml in which the following LinearLayout included:

portion_picker.xml

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="64dp" android:gravity="center" android:orientation="horizontal"> <ImageButton android:id="@+id/portion_minus" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@null" android:src="@drawable/ic_minus"/> <TextView android:id="@+id/potion_count_text_view" android:layout_width="64dp" android:layout_height="wrap_content" //64dp android:text="0" android:textSize="54sp" android:gravity="center"/> <ImageButton android:id="@+id/portion_plus" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@null" android:src="@drawable/ic_plus"/> </LinearLayout> 

The preview looks correct:

enter image description here

But on real devices (SGS5, Note 3), the TextView displays the text inside (in my case, the truth is just one / two digits) incorrectly - as you can see - not in the center (relative to the horizontal axis), but slightly lower, I think it is noticeable and hurts the eye.

enter image description here

Why does the text shift inside? In such a mapping, this is very critical, I ask you to tell why this offset occurs.

  • It is better not to preview the preview, but check it immediately on a real device. And try to center by putting android:gravity="center_vertical" for TextView - YuriySPb
  • This happens because the sizes (textSize) are given in relative units, which different screens interpret in their own way. In order to center everything you need to use such textView parameters as layout_gravity (alignment of textView to layout) and gravity (alignment of text inside textView) - iamthevoid
  • I encountered a similar problem when using Font. There was exactly the same effect as yours, but since he equally shifted all the elements, Margin saved me. - Eugene Troyanskii
  • @YuriySPb unfortunately the change from center to center_vertical did not help - abbath0767
  • @iamthevoid didn’t understand your advice at all, what do you want to inform by this, because I already use these parameters and define them - abbath0767

0