I'm developing an native android App, And I faced an issue that text in TextView is not placed in the center_vertical. Text is already placed in the center_horizental. The problem is vertical ...
Of course, the text tends to be centered vertically. However, it is located slightly below the exact center.
This is the code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:paddingBottom="16dp"
android:layout_gravity="center"
android:id="@+id/calendarLinear"
android:layout_width="match_parent"
android:layout_height="74dp">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/black_7" />
<TextView
android:id="@+id/tvDate"
tools:text="30"
android:layout_gravity="center"
android:textColor="@color/black_1"
android:textSize="12sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tvCheckIn"
tools:text="8:30"
android:layout_marginTop="4dp"
android:layout_marginHorizontal="4dp"
android:gravity="center"
android:layout_gravity="center|center_horizontal"
android:background="@drawable/erp_rounded_primary_bg"
android:textColor="@color/white"
android:textSize="12sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tvCheckout"
tools:text="18:30"
android:layout_marginTop="4dp"
android:layout_marginHorizontal="4dp"
android:gravity="center"
android:layout_gravity="center_vertical"
android:layout_centerInParent="true"
android:background="@drawable/erp_rounded_black4_bg"
android:textColor="@color/white"
android:textSize="12sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tvAttendGrp"
tools:text="출장"
android:visibility="gone"
android:layout_marginTop="2dp"
android:gravity="center"
android:textColor="@color/black_1"
android:fontFamily="sans-serif-medium"
android:paddingVertical="10dp"
android:textSize="12sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
The problem code is this
<TextView
android:id="@+id/tvCheckIn"
tools:text="8:30"
android:layout_marginTop="4dp"
android:layout_marginHorizontal="4dp"
android:gravity="center"
android:layout_gravity="center|center_horizontal"
android:background="@drawable/erp_rounded_primary_bg"
android:textColor="@color/white"
android:textSize="12sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tvCheckout"
tools:text="18:30"
android:layout_marginTop="4dp"
android:layout_marginHorizontal="4dp"
android:gravity="center"
android:layout_gravity="center_vertical"
android:layout_centerInParent="true"
android:background="@drawable/erp_rounded_black4_bg"
android:textColor="@color/white"
android:textSize="12sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
I tried using gravity, layout_gravity, and layout_centerInParent to fix it but didn't work.
What shold I add to place text in the center_vertical?
Thank you so much
The vertical alignment of the text is independent of the specific text. But "ÂÂÂ", "AAA", "aaa", "yyy" all have a different vertical "center", so the text may appear slightly off-center in a specific case. If you know that the text is a time, you can correct the deviation from the vertical center manually with e.g.
android:paddingBottom="0.8sp"