首页 文章

xml中的android字体 - 如何覆盖书法库中的默认字体

提问于
浏览
3

我试图换掉calligraphy(非常有用的库)为android fonts in xml但我不知道如何更改所有视图的默认字体,如书法 . 书法能够在初始化时更改默认字体 . 我怎么能用android字体支持库呢?

从android开发者网站上的xml文章中的字体我看到我们可以将字体系列应用到这样的一个视图:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fontFamily="@font/lobster"/>

但如果我想改变整个应用程序的字体怎么办?

我想使用android xml字体的原因是由于这个error但这与问题无关 .

1 回答

  • 0

    使用Support Library 26.1.0,我通过设置应用程序主题的 android:fontFamily 属性,设法为整个应用程序设置了默认字体:

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
         ...
        <item name="android:fontFamily">@font/proxima_nova</item>
    </style>
    

    其中 proxima_nova.xml 是字体资源文件:

    <?xml version="1.0" encoding="utf-8"?>
    <font-family xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        tools:ignore="UnusedAttribute">
        <!-- As of Android Support Library 26.0, we must declare both sets of attributes to ensure
             our fonts load on devices running Android 8.0 (API level 26) or lower. -->
        <font
            android:font="@font/proxima_nova_light"
            android:fontStyle="normal"
            android:fontWeight="300"
            app:font="@font/proxima_nova_light"
            app:fontStyle="normal"
            app:fontWeight="300" />
        <font
            android:font="@font/proxima_nova_semibold"
            android:fontWeight="600"
            app:font="@font/proxima_nova_semibold"
            app:fontWeight="600" />
    </font-family>
    

相关问题