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

我正在testingAndroid上的新工具栏和AppCompat主题,并遇到了一个问题。 我的工具栏标题文字在肖像模式下看起来正常,但在横向模式下它变得相当小,尽pipe我没有在代码中做任何事情来改变标题的文字大小。 这里是屏幕截图:

肖像景观

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> 

我试图设置工具栏的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> 

AOSP问题#170707是关于标题和副标题文本大小的变化。 项目成员的回应是“按预期工作,与框架行为相同”。 尽pipe我没有发现将文本大小更改为理想的默认行为,但听起来AppCompat的工程师必须与(有缺陷的)框架行为保持一致。 然后,开发人员可以按照Chilly Chan的回答来重写默认的样式。

除了Chilly Chan的回答:

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

2)纵向标题/字幕大小的默认值是20dp / 16dp(在我的Galaxy S3上,4.4.2)。 Chilly Chan的例子指定了“17sp”。 仅当您想要让用户偏好设置影响标题/字幕大小时才使用“sp”。

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

机器人:=了minHeight “机器人:ATTR / actionBarSize”

我也注意到你正在使用标准的黑暗动作栏,build议使用没有动作栏的主题,在其中定义了一个新的工具栏

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

我正在寻找一个没有自定义工具栏的解决scheme,但与自定义样式 ,这个代码做了我的伎俩:

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上进行testing。

我认为它与您在旋转设备时执行的一些布局更改有关,因为您似乎可以通过添加类似的东西来防止resize

  android:configChanges="orientation|screenSize" 

在AndroidManifest.xml中进行你所在的活动。与往常一样,android:configChanges有更多的含义,所以只有当你真的需要的时候才能使用它:)