Android支持v7工具栏setTitleTextAppearance无法正常工作 [英] Android Support v7 Toolbar setTitleTextAppearance not working

查看:149
本文介绍了Android支持v7工具栏setTitleTextAppearance无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更改工具栏标题的字体. 这是我的工具栏布局:

I am trying to change the font of the toolbar's title. Here is my toolbar layout:

<android.support.v7.widget.Toolbar 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    android:background="#666666"
    app:titleTextAppearance="@style/MyText"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

在我的styles.xml中

And in my styles.xml

<style name="MyText">
    <item name="fontPath">fonts/myFont.ttf</item>
</style>

如果未设置app:titleTextAppearance,则工具栏将使用系统字体.当我设置它时,字体变小,但是它仍然在系统字体中.我在做错什么吗?

If I do not set app:titleTextAppearance, the toolbar uses the system font. When I set it, the font gets smaller, but it is still in the system font. Am I doing something wrong?

任何建议,评论或答案都值得赞赏.

Any suggestion, comments or answers much appreciated.

我尝试将样式移至styles-text.xml,但没有运气

I tried moving the style to styles-text.xml but no luck

目前,我正在使用SpannableString和 TypefaceSpan 使它起作用.希望它能对某人有所帮助.

For now, I am using a SpannableString and TypefaceSpan to make this work. Hope it helps someone.

推荐答案

这有效:

<android.support.v7.widget.Toolbar 
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"

   android:layout_height="wrap_content"
   android:layout_width="match_parent"
   app:titleTextAppearance="@style/ActionBarTitleStyle"/>

然后在您的styles.xml中添加它

Then have this in your styles.xml

<style name="ActionBarTitle" parent="android:Widget.TextView">

    <!-- The action bar title font -->
    <item name="android:fontFamily">sans-serif-light</item>

    <!-- Customizes the color of the action bar title-->
    <item name="android:textColor">#FF0000</item>

    <!-- Customizes the size of the action bar title-->
    <item name="android:textSize">24sp</item>

</style>

这篇关于Android支持v7工具栏setTitleTextAppearance无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆