Android工具栏:横向模式下的小标题文本
我正在Android上测试新的工具栏和AppCompat主题,遇到了一个问题。我的工具栏标题文本在纵向模式下看起来大小正常,但在横向模式下它变得相当小,尽管我没有在代码中做任何更改标题的文本大小。以下是屏幕截图: activity_main.xml:Android工具栏:横向模式下的小标题文本,android,android-styles,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 t
<!-- 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” />
Toolbar-Toolbar=(Toolbar)findViewById(R.id.my\u-Toolbar);
设置支持操作栏(工具栏);
我试图设置工具栏的android:titletextearance
,但没有应用该样式。然后我意识到我使用的是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>
20便士
我认为这与旋转设备时执行的一些布局更改有关,但看起来可以通过添加以下内容来防止调整大小
android:configChanges="orientation|screenSize"
在您所在活动的AndroidManifest.xml中。像往常一样,android:configChanges有更多的含义,因此只有在您真正需要它时才应该使用:)是关于标题和副标题文本大小的更改而编写的。项目成员的回答是“按预期工作。与框架行为相同”。虽然我不认为更改文本大小是理想的默认行为,但听起来AppCompat工程师必须与(有缺陷的)框架行为保持一致。然后让开发人员覆盖Chilly Chan的回答中描述的默认样式
对Chilly Chan回答的补充:
1) 通过定义从TextAppearance.Widget.AppCompat.Toolbar.subtitle派生的另一种样式,可以以类似的方式控制字幕文本大小
2) 纵向标题/副标题大小的默认值为20dp/16dp(在我的Galaxy S3上,4.4.2.)。Chilly Chan的例子指定了“17sp”。仅当您想让用户首选项设置影响标题/副标题大小时才使用“sp”。我正在寻找一种解决方案不带自定义工具栏,但带有自定义样式,此代码为我提供了窍门: styles.xml
@颜色/原色
@颜色/原色暗
@颜色/颜色重音
@样式/MyActionBar
@样式/MyTitleTextStyle
20便士
AndroidManifest.xml
如果主活动扩展了主活动;在API 19、22和23上测试。谢谢,但它仍然很小。是否使用非黑色动作条主题?在模拟器或手机上运行?再次感谢。我用你发布的工具栏xml尝试了“Theme.AppCompat.Light.NoActionBar”,但标题仍然很小。唯一的区别是溢出图标和标题变为黑色。在电话上跑步。在ICS、KK和L上测试。在横向模式下,所有三款手机上的文本都很小。谢谢你的帮助。我已经解决了这个问题,并为这个问题添加了我自己的答案。我使用的是
android:titletextearance
而不是app:titletextearance
。不要在文本大小上使用dp。@qbix感谢您指出dp。dp是带有字幕的工具栏的正确选择,模仿谷歌的默认值。当用户启用“大文本可访问性”设置时,在工具栏中使用sp会导致字幕文本的下半部分被裁剪。另一种选择是允许工具栏拉伸到某个意外的高度以适合文本,即使如此,字幕文本填充也会变得不正确(与工具栏底部齐平,底部填充为0),或者至少是一个管理难题。非常好的解决方案!文本大小应为20sp,如纵向模式文本大小如何将其应用于standar support actionbar?(没有工具栏)@ChillyChan给你。我的xml文件没有什么特别之处。我根本不使用工具栏小部件。只是支持actionbar@color/primary\u color@color/primary\u dark\u color@color/accent\u color@color/gray@ChillyChan在注释中的代码格式有点不好,但我想要的是在横向和纵向上都有相同的actionbar标题文本大小。原因是大小设置为14dp in横向模式:android sdk/platforms/android-23/data/res/values land/dimens_material.xml 14dpHi all。我不知道为什么这个评论被否决了。我尝试了configChanges
,它在xml中没有任何其他更改的情况下工作。谢谢你@弗朗西丝科马佩利桑克斯。我也想知道投票失败的原因。。。也许我错过了一些重要的事情,为什么这不好。我知道我的解决方案与其说是一个干净的解决方案,不如说是一个变通办法,但至少它能起作用并给出原因的提示。这个答案是不正确的,因为文本大小会因设备在查看时(通常是应用程序启动时)的方向而不同。因此,如果你在纵向启动应用程序,你的工具栏将有一个更大的文本大小,通过旋转保持不变。但是如果你在场景中启动应用程序,你的工具栏会有更小的文本大小,再次通过旋转来保持。这就是为什么我添加了“一如既往地,Android:配置更改有更多的含义”警告……而不是使文本大小变大,你是否应该考虑使工具栏的高度变小?这就是谷歌在过去十年所做的
<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>
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/MyTheme"/>