android fonts in xml - 如何像书法库一样覆盖默认字体

android fonts in xml - how to override default font like calligraphy library does

我正在尝试换出 calligraphy (very useful library) for 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 但这与问题无关。

借助支持库 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>