gpt4 book ai didi

android - Oreo 不支持的字体

转载 作者:搜寻专家 更新时间:2023-11-01 08:21:17 26 4
gpt4 key购买 nike

我有一个应用程序,其中有一个名为“Crilee”的字体,它在其他设备上运行良好,但在 Android 的 Oreo 版本中运行不佳。我使用 settypeface() 来设置字体。我能知道为什么某些字体在 Oreo 版本和某些设备中不受支持吗?

最佳答案

Android 8.0(API 级别 26) 引入了一项新功能,Fonts in XML ,它允许您将字体用作资源。

将您的字体文件添加到res 目录下的font 文件夹中。

enter image description here

然后,在font文件夹下创建一个字体系列xml文件。
<font> 中包含每个字体文件、样式和粗细属性元素如下例所示。

<?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 布局中使用字体

在布局 XML 文件中,设置 fontFamily属性到您要访问的字体文件。

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

以编程方式使用字体

要以编程方式检索字体,请调用 getFont(int)方法并提供要检索的字体的资源标识符。

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

注意:要在运行 Android 4.1(API 级别 16)及更高版本的设备上使用 XML 中的字体功能,use the Support Library 26 .

关于android - Oreo 不支持的字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50541374/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com