Android Toolbar: small title text in landscape mode

家住魔仙堡 提交于 2019-11-26 10:26:01

问题


I\'m testing the new Toolbar and AppCompat theme on Android and ran into a problem. My toolbar title text looks normal-sized on portrait mode but it became rather small on landscape mode although I didn\'t do anything in the code to change the title\'s text size. Here are the screen shots:

\"Portrait\"\"Landscape\"

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>

回答1:


I tried to set android:titleTextAppearance of the toolbar but the style wasn't being applied. Then I realized I'm using the AppCompat theme so I used app:titleTextAppearance and the style is now being applied. It looks like the small letters in landscape are a problem in the built-in AppCompat.Toolbar.Title style itself so I overrode it to set the font size manually. The final code:

Toolbar 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" />

Toolbar Style:

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



回答2:


AOSP Issue #170707 was written regarding the change in text size for title and subtitle. Project Member response was "Works as intended. Identical to framework behavior." Although I don't find changing the text size to be the desirable default behavior, it sounds like the AppCompat engineers had to maintain consistency with the (flawed) framework behavior. Developers are then left to override the default styles as described in Chilly Chan's answer.

Additions to Chilly Chan's answer:

1) The subtitle text size can be controlled in a similar way by defining another style derived from TextAppearance.Widget.AppCompat.Toolbar.Subtitle.

2) Default values for title/subtitle size in portrait orientation are 20dp/16dp (on my Galaxy S3, 4.4.2.). Chilly Chan's example specifies "17sp". Use "sp" only if you want to let user preference setting affect title/subtitle size.




回答3:


I was looking for a solution without custom Toolbar, but with custom style and this code did the trick for me:

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"/>

Where the MainActivity extends AppCompatActivity; tested on API 19, 22 and 23.




回答4:


Try to add this to your toolbar section under the activity_main.xml.

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

I also noticed that you are using standard dark action bar , suggest to use Theme with no action bar , defined a new toolbar where

 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” />



回答5:


I think it has to do with some layout changes performed when you rotate the device, ast it looks like you can prevent the resize by adding something like

  android:configChanges="orientation|screenSize"

in the AndroidManifest.xml for the activity you're in. As always, android:configChanges has more implications so should be used only if you really need it :)



来源:https://stackoverflow.com/questions/28042331/android-toolbar-small-title-text-in-landscape-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!