我正在尝试换出 calligraphy (非常有用的库)for android fonts in xml 但我不知道如何更改所有 View 的默认字体,如书法。书法能够在初始化时更改默认字体。我如何使用 Android 字体支持库执行此操作?
从 android 开发者网站上的 xml 中的字体文章我看到我们可以像这样将字体系列应用于一个 View :
<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>
关于xml中的android字体 - 如何像书法库一样覆盖默认字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46509711/