Android 如何自定义操作栏字幕字体?
我创建了一个ActionBar(Android 如何自定义操作栏字幕字体?,android,android-actionbar-compat,android-toolbar,android-appcompat,Android,Android Actionbar Compat,Android Toolbar,Android Appcompat,我创建了一个ActionBar(android.support.v7.widget.Toolbar),如下所示 <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/myToolbar" android:layout_width="match_parent" android:layout_he
android.support.v7.widget.Toolbar
),如下所示
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myToolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize">
</android.support.v7.widget.Toolbar>
在我的动作栏里,除了标题,我还有副标题。但是我想定制字幕。在自定义中,我指的是字体大小、颜色和字体
我尝试过各种主题和风格,但仍然不成功
如果有一个简单完整的例子来说明如何做到这一点,那将非常有帮助。谢谢 如果布局文件中还没有此命名空间属性,请添加它: xmlns:app=”http://schemas.android.com/apk/res-auto" 按如下方式更新布局文件中的工具栏属性:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myToolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:titleTextAppearance="@style/ToolbarTitleAppearance"
app:subtitleTextAppearance="@style/ToolbarSubtitleAppearance"
android:minHeight="?attr/actionBarSize">
</android.support.v7.widget.Toolbar>
添加如下样式:
<style name="ToolbarTitleAppearance" parent="@style/TextAppearance.Widget.AppCompat.Toolbar.Title">
<item name="android:textSize">20dp</item>
<!-- include other attributes you want to change: textColor, textStyle, etc -->
</style>
<style name="ToolbarSubtitleAppearance" parent="@style/TextAppearance.Widget.AppCompat.Toolbar.Subtitle">
<item name="android:textSize">14dp</item>
</style>
20dp
14dp
对此答案的评论讨论了
textSize
的单位是dp
还是sp
。当我最初写这个答案时,我查看了Android源文件,发现使用了dp
。我建议你留下来。正如Francescondozello在他的评论中解释的那样,工具栏的大小是固定的,与其他小部件不同,它不会扩展到包含通过更改手机设置而放大的文本。太棒了!谢谢我几乎选择了不太理想的解决方案,即getSupportActionBar().setSubtitle(Html.fromHtml(“+title+”)代码>对于文本大小,应使用sp而不是dp@FrancescoDonzello检查Android源代码,他们使用dp作为工具栏文本大小。这很有意义,因为在手机设置中更改基本字体大小时,工具栏高度不会改变。