Android自定义工具栏标题文字不以Android 4.4设备为中心

时间:2017-09-21 13:24:27

标签: android-toolbar android-4.4-kitkat

我为自己的应用创建了自定义工具栏。但由于某些原因我在Android 4.4 KitKat设备上未知,垂直居中的文本没有正确居中。在所有Android 5+设备上,它工作得很好。

似乎在4.4中,textView文本的底部(而不是中心)与工具栏的中心对齐。

任何人都知道如何解决这个问题?

如果你想看到xml,请点击这里:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/toolbar_root"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            tools:background="@color/background_default">

<LinearLayout
    android:id="@+id/toolbar_icon_left_container"
    android:layout_width="48dp"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:background="?selectableItemBackgroundBorderless"
    android:clickable="true">

    <ImageView
        android:id="@+id/toolbar_icon_left_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:src="@drawable/back"/>

</LinearLayout>

<TextView
    android:id="@+id/toolbar_title"
    android:fontFamily="@font/museo700"
    android:singleLine="true"
    android:layout_toRightOf="@+id/toolbar_icon_left_container"
    android:layout_toLeftOf="@+id/toolbar_text_right"
    style="@style/TextStyle5"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_toEndOf="@+id/toolbar_icon_left_container"
    android:gravity="center_vertical"
    tools:text="Selecteer een leverdatum"/>

<TextView
    android:id="@+id/toolbar_text_right"
    style="@style/TextStyle7"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_alignParentRight="true"
    android:layout_marginRight="@dimen/screen_edge_margin"
    android:background="?selectableItemBackgroundBorderless"
    android:clickable="true"
    android:gravity="right|center_vertical"
    android:minWidth="48dp"
    tools:gravity="right|center_vertical"
    tools:text="Help"/>

1 个答案:

答案 0 :(得分:0)

修正了它:textStyle包含 lineSpacingExtra

的值
<item name="android:lineSpacingExtra">10sp</item>

这导致TextView在TextView底部添加额外的边距,因此不会在工具栏中正确居中文本。

这只发生在KitKat&amp;不受此影响的棒棒糖设备。