【发布时间】:2016-11-14 11:29:18
【问题描述】:
我有一个自定义扩展 TextView 我在我的应用程序中用于自定义字体,但由于某种原因,我不断收到运行时异常,程序无法找到相关字体。
我使用的目录格式是main > assets > fonts > Portrait-Light.ttf
我到处寻找解决方案,但他们似乎都在四舍五入到 SO 上的相同答案。
CustomFontTextView.java:
public class CustomFontTextView extends TextView {
public CustomFontTextView(Context context) {
super(context);
applyCustomFont(context);
}
public CustomFontTextView(Context context, AttributeSet attrs) {
super(context, attrs);
applyCustomFont(context);
}
public CustomFontTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
applyCustomFont(context);
}
private void applyCustomFont(Context context) {
Log.e("it gets here", "custom font");
Typeface customFont = FontCache.getTypeface("Roboto-Italic.ttf", context);
setTypeface(customFont);
}
}
FontCache.java
class FontCache {
private static HashMap<String, Typeface> fontCache = new HashMap<>();
static Typeface getTypeface(String fontname, Context context) {
Typeface typeface = fontCache.get(fontname);
if (typeface == null) {
try {
typeface = Typeface.createFromAsset(context.getAssets(), "fonts/" + fontname);
} catch (Exception e) {
Log.e("failed", e.getMessage());
}
fontCache.put(fontname, typeface);
}
return typeface;
}
}
XML:
<icn.premierandroid.misc.CustomFontTextView
android:id="@+id/switch_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textSize="14sp"
android:textColor="@color/colorPrimary"
android:gravity="center_horizontal"
android:text="@string/are_you_over_18_years_old"/>
我已经尝试过使用不同的格式(例如 .otf)和不同的字体(例如 Roboto-Italic.ttf)以及来自 dafont.com 的另一个名为 Sunset-Clouds.ttf 的随机字体,但我仍然收到错误消息,这是怎么回事?这应该工作。为了以防万一,我什至更新了所有插件,例如 Gradle、Grade-Wrapper 分发和 Android gradle 插件。
我也尝试过单一的方法:
AssetManager am = context. getApplicationContext(). getAssets();
Typeface font = Typeface.createFromAsset(
am, String.format(Locale.US, "fonts/%s", "portrait-light.ttf"));
我错过了什么吗?
更新:
删除捕获会显示此堆栈跟踪。
进程:icn.premierandroid,PID:3829 java.lang.RuntimeException:无法启动活动 ComponentInfo{icn.premierandroid/icn.premierandroid.RegisterActivity}: android.view.InflateException:二进制 XML 文件第 100 行:错误 膨胀类 icn.premierandroid.misc.CustomFontTextView 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2702) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2767) .....
原因:android.view.InflateException:二进制 XML 文件第 100 行: 膨胀类 icn.premierandroid.misc.CustomFontTextView 时出错 在 android.view.LayoutInflater.createView(LayoutInflater.java:640) 在 android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:750) 在 android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
.....
引起:java.lang.reflect.InvocationTargetException 在 java.lang.reflect.Constructor.newInstance(Native Method) 在 java.lang.reflect.Constructor.newInstance(Constructor.java:288) 在 android.view.LayoutInflater.createView(LayoutInflater.java:614)
....
原因:java.lang.RuntimeException:找不到字体资源 字体/GleegooRegular.ttf 在 android.graphics.Typeface.createFromAsset(Typeface.java:272) 在 icn.premierandroid.misc.FontCache.getTypeface(FontCache.java:21) 在 icn.premierandroid.misc.CustomFontTextView.applyCustomFont(CustomFontTextView.java:28) 在 icn.premierandroid.misc.CustomFontTextView.(CustomFontTextView.java:18) 在 java.lang.reflect.Constructor.newInstance(Native Method)
.....
更新 2:
好的,所以有一个奇怪的解决方法。当您将assets 文件夹直接添加到app/src/main 内的main 文件夹中时,显然android studio 不喜欢它。您需要先将其添加到app/src/main/res 文件夹中,然后将其移动到main 文件夹中。不知道为什么会这样,但它解决了我的问题。
【问题讨论】:
-
请发布错误堆栈跟踪
-
@maxost,给你加了,老兄。
-
“字体资源未找到字体/GleegooRegular.ttf”,我不确定,但我在您的字体目录中没有看到此字体。
-
它是在之后添加的,抱歉我在等待答案时正在调试它(只是尝试一种新字体)
标签: java android fonts android-assets custom-font