Android安卓中高效率替换整个APP应用字体——全局字体Fonts in XML

Android O推出了一项新的功能「Fonts in XML」,借助这项功能,我们能够像使用其他资源文件一样使用字体,比较方便地实现App全局字体的替换。

为了能够在API 14或者以上的设备上使用Fonts in XML特性,我们需要使用到Support Library 26。更多的内容可以参考「使用Support Library」小节。

在Android Studio中按照如下步骤将字体作为资源文件添加至工程:

  1. 右键单击项目的app / res文件夹,然后选择New > Android resource directory。
  2. 打开下拉菜单并选择font,输入font作为File name,点击OK。
    注意名称字体资源文件夹的名称必须为font
    在这里插入图片描述
  3. 将字体文件拖放到新的res / font文件夹中。Android O支持.otf(OpenType)和.ttf(TrueType)两种格式的字体文件。
    在这里插入图片描述
  4. 双击字体文件可以在编辑器中对字体进行预览。
    在这里插入图片描述

创建Font family

在Android Studio中创建Font family的步骤如下:

  1. 右键单击项目的res / font文件夹,然后选择New > Font resource file。
  2. 输入文件名,然后点击OK.
  3. 打开此XML文件并定义该字体的所有不同版本,以及其样式和权重属性,例如:
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android">
    <font
        android:fontStyle="normal"
        android:fontWeight="400"
        android:font="@font/lobster_regular" />
    <font
        android:fontStyle="italic"
        android:fontWeight="400"
        android:font="@font/lobster_italic" />
</font-family>

在XML布局中使用字体资源

给TextView添加字体
• 在XML布局文件中,将fontFamily设置为你想要的访问的字体文件:

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

• 打开Properties 窗口,设置TextView的字体:

  1. 选择一种视图打开Properties窗口
  2. 展开textAppearance,选择fontFamily表中的一种字体类型。
    在这里插入图片描述
    在这里插入图片描述

添加字体至style

打开style.xml文件,将fontFamily属性设置为你想要访问的字体文件。

<style name="customfontstyle" parent="@android:style/TextAppearance.Small">
    <item name="android:fontFamily">@font/lobster</item>
</style>

在你的App的Theme中配置此属性即可实现整个App的字体替换。

使用代码控制字体

Typeface typeface = getResources().getFont(R.font.myfont);
textView.setTypeface(typeface);

使用Support Library

当我们通过Support Library实现Fonts in XML特性时,需要使用app命名空间。Support Library目前支持API 14及以上。

在Android Support Library 26.0-beta1中,必须同时使用android和app命名空间进行声明,以确保在Android O版本及以下设备上字体能够被正确加载。

<?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">
    <font android:fontStyle="normal" android:fontWeight="400" android:font="@font/myfont-Regular"
          app:fontStyle="normal" app:fontWeight="400" app:font="@font/myfont-Regular"/>
    <font android:fontStyle="italic" android:fontWeight="400" android:font="@font/myfont-Italic"
          app:fontStyle="italic" app:fontWeight="400" app:font="@font/myfont-Italic" />
</font-family>

通过代码控制:

Typeface typeface = ResourcesCompat.getFont(context, R.font.myfont);

内容均来自Android Developer官网,做了简单的翻译,水平有限,还请见谅,原地址: https://developer.android.com/preview/features/fonts-in-xml.html
参考: https://developer.android.com/preview/features/working-with-fonts.html

另外,我在我的开源项目TonnyL/PaperPlane 中使用Fonts in XML实现了App的字体的整体替换。效果如下:
在这里插入图片描述
本文参考来源:
[1]Android如何高效率的替换整个APP的字体?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值