xml 字体和可下载字体
Android O 推出了 xml 字体,可以在资源文件中建立 font 字体资源文件夹,放入相应的字体 ttf 文件,然后建立自己的字体 xml 文件,在 R 文件中编译,最终作为一种资源供 TextView 等使用,
<?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>
同时 Android O 和 Android support library 26 提供了一个 API,可以从另一个应用中获取字体资源,而不是将字体资源打包到 apk 中或者让 apk 去下载,通过 Android support library 26 版本可以让这个特性支持到 14 版本或者之上的设备上,具体的 API 可以看看这个:Downloadable Fonts 或者这个 sample android-DownloadableFonts:
[图片上传中...(image-aa4984-1590288997214-0)]
适用场景:这个比较适合在公司的一系列 APP 上使用,更换皮肤同时更换字体等等。
此示例演示了如何使用 Android O 中引入的可下载字体功能。通过可下载字体功能,应用可向提供商请求特定字体,而无需打包字体或自行下载字体。这意味着,不再需要将字体打包为资源。
网友评论