Android支持v7工具栏setTitleTextAppearance无效

时间:2014-12-17 16:27:19

标签: android android-appcompat android-toolbar

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

<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,但没有运气

EDIT2:

目前,我正在使用SpannableString和TypefaceSpan来完成这项工作。希望它可以帮到某人。

2 个答案:

答案 0 :(得分: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: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 :(得分:0)

就我而言,只有当我使用&#34; android:titleTextAppearance &#34;时才会应用font:family。代替&#34; app:titleTextAppearance &#34;但这仅适用于api 21或更高版本