Android支持v7工具栏setTitleTextAppearance无效。

3
我正在尝试更改工具栏标题的字体。 这是我的工具栏布局:
<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 文件中

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

如果我不设置app:titleTextAppearance,工具栏将使用系统字体。当我设置它时,字体变小了,但仍然是系统字体。我做错了什么吗?
非常感谢任何建议、评论或答案。
编辑:
我尝试将样式移动到styles-text.xml,但没有成功。
编辑2:
目前,我正在使用SpannableString和TypefaceSpan来解决这个问题。希望能对某些人有所帮助。

它不会改变字体。 - luksfarris
2个回答

5

这个可以正常工作:

<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文件中添加以下内容:
<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>

1
在我的情况下,只有当我使用 "android:titleTextAppearance" 而不是 "app:titleTextAppearance" 时才会应用字体样式,但这仅适用于 API 21 或更高版本。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接