gpt4 book ai didi

java - 从资源获取字体在 Java 中存在错误 - Android

转载 作者:行者123 更新时间:2023-12-02 09:03:17 27 4
gpt4 key购买 nike

我编写代码并使用 CompatResources 获取字体并设置为 TypeFace 以查看 Bug Fabric.io 报告了一些有关它的崩溃。

Caused by android.content.res.Resources$NotFoundException Font resource ID #0x7f090000 could not be retrieved.

这是我的代码:

tfFontIcon = ResourcesCompat.getFont(mContext, R.font.font_icon);

感谢您的帮助。

最佳答案

尝试以下灵魂

解决方案1

将您的字体添加到 Assets 文件夹中,如下图所示

enter image description here

并使用下面的方法设置字体 typeFace

CommonUtils.setFont(context, binding.txtDigital, "Montserrat-Bold.ttf");

public static void setFont(Context context, TextView textView, String fontPath) {
Typeface t = Typeface.createFromAsset(context.getResources().getAssets(), fontPath);
textView.setTypeface(t);
}

解决方案2

res 文件夹中添加字体文件夹,如下图所示

enter image description here

并直接将字体系列添加到 TextView 中

<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/txtTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/app_name"
android:fontFamily="@font/roboto_medium"
android:textColor="@color/black"
android:textSize="@dimen/_20ssp"
android:visibility="gone" />

希望对您有帮助!

谢谢。

关于java - 从资源获取字体在 Java 中存在错误 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60014664/

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