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

Nye*_*ynn 58 android android-styles

我在Android上测试新的工具栏和AppCompat主题并遇到了问题.我的工具栏标题文本在纵向模式下看起来是正常大小但在横向模式下变得相当小,尽管我在代码中没有做任何改变标题文本大小的事情.以下是截图:

肖像 景观

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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

Nye*_*ynn 105

我试图设置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" />
Run Code Online (Sandbox Code Playgroud)

工具栏样式:

<style name="ToolbarTitle" parent="@style/TextAppearance.Widget.AppCompat.Toolbar.Title">
        <item name="android:textSize">20sp</item>
</style>
Run Code Online (Sandbox Code Playgroud)

  • 很好的解决方案!textSize应为20sp,作为纵向模式文本大小 (4认同)

Bob*_*der 11

AOSP问题#170707是关于标题和副标题的文本大小的变化而写的.项目成员的回应是"按预期工作.与框架行为相同".虽然我没有发现将文本大小更改为理想的默认行为,但听起来AppCompat工程师必须保持与(有缺陷的)框架行为的一致性.然后开发人员将覆盖Chilly Chan的答案中描述的默认样式.

Chilly Chan回答的补充:

1)通过定义从TextAppearance.Widget.AppCompat.Toolbar.Subtitle派生的另一个样式,可以以类似的方式控制字幕文本大小.

2)纵向标题/字幕大小的默认值为20dp/16dp(在我的Galaxy S3上,4.4.2.).Chilly Chan的例子指的是"17sp".仅当您要让用户首选项设置影响标题/字幕大小时,才使用"sp".

  • 不要在文本大小上使用dp. (2认同)

Mic*_*ran 8

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

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>
Run Code Online (Sandbox Code Playgroud)

AndroidManifest.xml中

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/MyTheme"/>
Run Code Online (Sandbox Code Playgroud)

MainActivity扩展AppCompatActivity的位置; 在API 19,22和23上测试.