Android 工具栏:横向模式下的小标题文本

Android Toolbar: small title text in landscape mode

我在 Android 和 运行 上测试新的 Toolbar 和 AppCompat 主题时遇到问题。我的工具栏标题文本在纵向模式下看起来 normal-sized,但在横向模式下它变得相当小,尽管我没有在代码中做任何改变标题文本大小的事情。以下是屏幕截图:

activity_main.xml:

<!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. -->
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="com.techfunmyanmar.jujaka.ui.MainActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/main_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <!-- As the main content view, the view below consumes the entire
             space available using match_parent in both dimensions. -->
        <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <!-- android:layout_gravity="start" tells DrawerLayout to treat
             this as a sliding drawer on the left side for left-to-right
             languages and on the right side for right-to-left languages.
             If you're not building against API 17 or higher, use
             android:layout_gravity="left" instead. -->
        <!-- The drawer is given a fixed width in dp and extends the full height of
             the container. -->
        <fragment
            android:id="@+id/navigation_drawer"
            android:name="com.techfunmyanmar.jujaka.ui.NavigationDrawerFragment"
            android:layout_width="@dimen/navigation_drawer_width"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            tools:layout="@layout/fragment_navigation_drawer" />
    </android.support.v4.widget.DrawerLayout>
</LinearLayout>

styles.xml:

<resources>
    <!-- Base application theme. -->
    <style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="windowActionBar">false</item>

        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/primary</item>
        <item name="colorPrimaryDark">@color/primary_dark</item>
        <item name="colorAccent">@color/accent</item>

    </style>

    <!-- Main application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">

    </style>

    <style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
        <item name="spinBars">true</item>
    </style>
</resources>

尝试将其添加到 activity_main.xml 下的工具栏部分。

android:minHeight="?android:attr/actionBarSize"

我还注意到您使用的是标准的深色操作栏,建议使用没有操作栏的主题,定义了一个新的工具栏,其中

 Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
    setSupportActionBar(toolbar);

<android.support.v7.widget.Toolbar
    android:id=”@+id/my_awesome_toolbar”
    android:layout_height=”wrap_content”
    android:layout_width=”match_parent”
    android:minHeight=”?attr/actionBarSize”
    android:background=”?attr/colorPrimary” />

我尝试设置工具栏的 android:titleTextAppearance,但未应用样式。然后我意识到我正在使用 AppCompat 主题,所以我使用了 app:titleTextAppearance 并且现在正在应用样式。看起来横向的小字母是内置 AppCompat.Toolbar.Title 样式本身的问题,所以我覆盖它以手动设置字体大小。最终代码:

工具栏XML:

<android.support.v7.widget.Toolbar
        android:id="@+id/main_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:titleTextAppearance="@style/ToolbarTitle"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

工具栏样式:

<style name="ToolbarTitle" parent="@style/TextAppearance.Widget.AppCompat.Toolbar.Title">
        <item name="android:textSize">20sp</item>
</style>

我认为这与旋转设备时执行的一些布局更改有关,因为看起来您可以通过添加类似

的内容来阻止调整大小
  android:configChanges="orientation|screenSize"

in the AndroidManifest.xml for the activity you're in. 一如既往,android:configChanges 有更多的含义,所以只有当你真的需要它时才应该使用它:)

AOSP Issue #170707 是关于更改标题和副标题的文字大小的。项目成员的回应是 "Works as intended. Identical to framework behavior." 虽然我没有发现更改文本大小是理想的默认行为,但听起来 AppCompat 工程师必须保持与(有缺陷的)框架行为的一致性。然后开发人员可以按照 Chilly Chan 的回答中所述覆盖默认样式。

Chilly Chan 回答补充:

1) 可以通过定义另一种派生自 TextAppearance.Widget.AppCompat.Toolbar.Subtitle.

的样式,以类似的方式控制字幕文本大小

2) title/subtitle 纵向大小的默认值为 20dp/16dp(在我的 Galaxy S3 4.4.2 上)。 Chilly Chan 的示例指定“17sp”。仅当您想让用户首选项设置影响 title/subtitle 大小时才使用 "sp"。

我一直在寻找一种解决方案没有自定义工具栏,但是有自定义样式,这段代码对我有用:

styles.xml

<style name="MyTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="actionBarStyle">@style/MyActionBar</item>
</style>

<style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid">
    <item name="titleTextStyle">@style/MyTitleTextStyle</item>
</style>

<style name="MyTitleTextStyle" parent="@style/TextAppearance.AppCompat.Title">
    <item name="android:textSize">20sp</item> <!-- Default for portrait is 20sp and for landscape 14sp-->
</style>

AndroidManifest.xml

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/MyTheme"/>

MainActivity 扩展了 AppCompatActivity;在 API 19、22 和 23 上测试。