Android Support v7 Toolbar setTitleTextAppearance not working

巧了我就是萌 提交于 2019-12-13 18:34:51

问题


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"/>

And in my styles.xml

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

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.

Edit:

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

Edit2:

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


回答1:


This works:

<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"/>

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>



回答2:


In my case, the font:family is only applied if I use "android:titleTextAppearance" instead of "app:titleTextAppearance" but this only works for api 21 or higher



来源:https://stackoverflow.com/questions/27530215/android-support-v7-toolbar-settitletextappearance-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!