gpt4 book ai didi

android - 使用的特定自定义字体在 Android 应用程序中不起作用

转载 作者:行者123 更新时间:2023-11-30 02:34:12 25 4
gpt4 key购买 nike

我在 TextView 中使用 DIEHL DECO 字体。我的代码中的类文件如下。应用程序运行没有任何错误,但显示的字体是默认字体。字体没有更改为我正在使用的字体文件。我尝试使用另一个字体文件,代码运行良好,但对于这种特定字体运行不佳。不明白问题是什么。请帮助我。

public class MyTextView extends TextView{

public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);

}

public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}

public MyTextView(Context context) {
super(context);

}

public void setTypeface(Typeface tf, int style) {
if (style == Typeface.BOLD) {
super.setTypeface(TypeFaces.getTypeFace(getContext(),
"fonts/DIEHLD_.ttf"));
} else if (style == Typeface.ITALIC) {
super.setTypeface(TypeFaces.getTypeFace(getContext(),
"fonts/DIEHLD_.ttf"));
} else {
super.setTypeface(TypeFaces.getTypeFace(getContext(),
"fonts/DIEHLD_.ttf"));
}
}

}

这是字体类

public class TypeFaces {
private static final Hashtable<String, Typeface> cache = new Hashtable<String, Typeface>();

public static Typeface getTypeFace(Context context, String assetPath) {
synchronized (cache) {
if (!cache.containsKey(assetPath)) {
try {
Typeface typeFace = Typeface.createFromAsset(
context.getAssets(), assetPath);
cache.put(assetPath, typeFace);
} catch (Exception e) {
Log.e("TypeFaces", "Typeface not loaded.");
return null;
}
}
return cache.get(assetPath);
}
}
}

最佳答案

如果您在 Assets 文件夹中创建了字体文件夹,请使用 Assets /字体/DIEHLD_.ttf。

关于android - 使用的特定自定义字体在 Android 应用程序中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26841577/

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