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

标签 android fonts calligraphy

我正在尝试换出 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/

相关文章:

css - 动态字体大小 Flex 4 可在调整窗口/面板大小时调整大小

android - 在带有 dexguard 的 Android 应用程序的发布版本中未应用书法字体

android - 无法解析符号 'CalligraphyContextWrapper'

android - 样式属性在 webview 中不起作用

android - 有没有办法让 WebView 小部件使用 Chrome 而不是三星/LG Android 手机上的内置 WebKit 浏览器?

html - CSS 中的@font-face 问题

css - @font-face 渲染为 Times New Roman

android - chrisjenx 的书法库无法正常工作

android - 如何通过操作栏中的后退按钮转到上一个 fragment ? ( Kotlin )

java - 逻辑错误;即使在设置为 null 后,TextView 也会返回