【问题标题】:Get font from resources has bug in Java - Android从资源中获取字体在 Java 中存在错误 - Android
【发布时间】:2020-05-17 18:13:14
【问题描述】:

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

由 android.content.res.Resources$NotFoundException 字体引起 无法检索资源 ID #0x7f090000。

这是我的代码:

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

感谢您的帮助。

【问题讨论】:

    标签: java android fonts resources typeface


    【解决方案1】:

    试试下面的灵魂

    解决方案 1

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

    并使用下面的方法来设置字体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 文件夹中添加字体文件夹,如下图所示

    并直接将字体系列添加到您的文本视图中

    <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" />
    

    希望对你有帮助!

    谢谢。

    【讨论】:

    • 有没有办法使用资源而不是资产?崩溃的原因是什么?
    • 我知道 xml 或 assets,但在以编程方式设置和使用资源时出现问题。在 java 中查看构建,这不是解决方案
    • 您可以添加添加字体文件的屏幕截图吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-23
    相关资源
    最近更新 更多