工具栏字幕被剪切(不适合高度)

Toolbar subtitle is cut (It does not fit in height)

我的问题很简单,但是我想不通:

当我尝试显示 副标题 时, 工具栏 没有足够的 space 显示它并且它被剪切了.

工具栏:

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="?android:actionBarSize"
android:layout_centerHorizontal="true"
app:theme="@style/MyToolBar">

?android:actionBarSize是否没有足够的高度来正确显示titlesubtitle

当我将 ToolBar 高度设置为 wrap_content 时一切正常,但谁能向我解释我应该如何正确设置高度?

styles.xml :

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:windowBackground">@color/background1</item>
    <item name="android:textColorPrimary">@color/colorAccent</item>

    <!-- Customize your theme here. -->
</style>

<style name="MyToolBar" parent="Widget.AppCompat.ActionBar">
    <!-- Support library compatibility -->
    <item name="android:textColorPrimary">@color/colorAccent</item>
    <item name="android:textColorSecondary">@color/colorAccent</item>
    <item name="colorControlNormal">@color/colorAccent</item>

</style>

我建议你为工具栏创建一个新的布局然后你可以填充它 但是您希望 to.And 在 java 代码中将其设置为 ActionBar。因此,您将不会再遇到任何 space 问题。

我使用了带字幕的工具栏,除了使用 layout_height="wrap_content",我还添加了 minHeight。这将确保您的工具栏至少是最小高度。我没有看到它跳高或裁剪,但我总是有字幕集。

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:elevation="@dimen/default_elevation"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

问题是您将布局高度指定为?attr/actionBarSize。如果您没有指定副标题,这应该没问题,因为这只是为标题量身定做的。我的建议是制作 android:layout_height="wrap_content" 而不是使用工具栏的 minHeight 参数。因此你的 xml 变成了-

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:layout_centerHorizontal="true"
app:theme="@style/MyToolBar">

我已经使用 layout_height=actionBarSize 很长时间了,即使有字幕也从未遇到过这个问题(在各种模拟器和像素 phone 上进行测试)。在看到我的字幕在 Samsung Galaxy phone 上被截断后,我现在应用了此修复程序。在我看来,这个问题取决于系统如何设置字幕间距。

我已经切换到新的 material 组件 (androidx)。所以这个问题在最新版本中仍然存在。

使用 wrap_content 作为高度和 actionBarSize 属性作为 minHeight 的组合的解决方案不会影响没有问题的设备,因此在这些设备上,不会发生跳跃可见。

<androidx.appcompat.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:actionBarSize" />

在我的例子中,我为我的项目使用了自定义字体 (NotosanJP),所以我的 Toolbar 副标题被删掉了。不使用自定义字体,效果很好。
所以我的解决方案是更改Toolbar.
中的文本大小 调试后发现我现在的Toolbar标题的默认大小是20,Toolbar副标题是16,所以我减少了2

<androidx.appcompat.widget.Toolbar
            ...
            app:titleTextAppearance="@style/MyToolbarTitleAppearance"
            app:subtitleTextAppearance="@style/MyToolbarSubtitleAppearance" />

style.xml

<style name="MyToolbarTitleAppearance" parent="@style/TextAppearance.Widget.AppCompat.Toolbar.Title">
    <item name="android:textSize">18dp</item>
</style>

<style name="MyToolbarSubtitleAppearance" parent="@style/TextAppearance.Widget.AppCompat.Toolbar.Subtitle">
    <item name="android:textSize">14dp</item>
</style>

除了已接受的答案建议添加 android:minHeight="?attr/actionBarSize",我还建议您添加底部填充。在我的例子中,如果我不这样做,根据系统字体大小,字幕几乎被工具栏的末尾切断。如果您有 space,只需添加 android:paddingBottom="6dp"