gpt4 book ai didi

android - 无法在 Android 中使用字体

转载 作者:行者123 更新时间:2023-11-30 01:16:10 25 4
gpt4 key购买 nike

我已经阅读了所有关于在 Android 中使用字体的 SO 帖子,但我仍然无法正常工作。我收到以下异常:

java.lang.RuntimeException: 无法制作原生字体

这是我的代码(此类和静态方法取自 here ):

public class SafeTypefaces {

private static final Hashtable<String, Typeface> cache = new Hashtable<String, Typeface>();

public static Typeface get(Context c) {

final String assetPath = "fonts/robotobold.ttf";

synchronized (cache) {
if (!cache.containsKey(assetPath)) {
try {
AssetManager assetManager = c.getAssets();
Typeface t = Typeface.createFromAsset(assetManager, assetPath); //Throws exception on this line!
cache.put(assetPath, t);
} catch (Exception e) {
L.p("Could not get typeface '" + assetPath + "' because " + e.getMessage());
return null;
}
}
return cache.get(assetPath);
}
}
}

字体在assets/fonts文件夹中:

enter image description here

(这仍然是 Eclipse。我知道,我知道)。

我尝试过的事情:

  • 我已尝试将字体移动到 assets 文件夹的根目录并更新 assetPath
  • 我检查了文件夹和文件的权限。
  • 我尝试使用应用程序上下文而不是我正在获取的 Activity 上下文。
  • 我尝试使用不同的字体文件(一个通过电子邮件发送,另一个从互联网下载)。

最佳答案

当我创建这个问题时,我遇到了解决方案。我在这上面花了几个小时,所以我想它值得保留。

我的问题是我正在处理一个图书馆项目。根据此 ( Android Library assets folder doesn't get copied ),库项目中的 Assets 不可用于主项目,这就是找不到字体文件的原因。 (同样,该项目仍在使用 Eclipse。可能不是使用 Gradle+AS 的情况)。

解决方案是将字体复制到主项目中,瞧!

关于android - 无法在 Android 中使用字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37841834/

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