我有同样的问题,我不相信它依赖于设备。
我通过确保以下内容解决了这个问题:
如果您有多个项目,请确保您的字体文件存储在主项目的资产文件夹中- 而不是依赖项目。
为了安全起见,请将您的字体重命名为全部小写并在您的代码中引用它。
FontUtils.setDefaultFont(this, "DEFAULT", "fonts/arimo-regular.ttf");
这是一个覆盖整个应用程序的默认字体的类。
public class FontUtils {
/**
* Sets the default font.
*
* @param context the context
* @param staticTypefaceFieldName the static typeface field name
* @param fontAssetName the font asset name
*/
public static void setDefaultFont(Context context,
String staticTypefaceFieldName, String fontAssetName) {
final Typeface regular = Typefaces.get(context, fontAssetName);
replaceFont(staticTypefaceFieldName, regular);
}
/**
* Replace a font.
*
* @param staticTypefaceFieldName the static typeface field name
* @param newTypeface the new typeface
*/
protected static void replaceFont(String staticTypefaceFieldName,
final Typeface newTypeface) {
try {
final Field StaticField = Typeface.class
.getDeclaredField(staticTypefaceFieldName);
StaticField.setAccessible(true);
StaticField.set(null, newTypeface);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
static class Typefaces {
private static final Hashtable<String, Typeface> cache = new Hashtable<String, Typeface>();
public static Typeface get(Context c, String assetPath) {
synchronized (cache) {
if (!cache.containsKey(assetPath)) {
try {
Typeface t = Typeface.createFromAsset(c.getAssets(),
assetPath);
cache.put(assetPath, t);
} catch (Exception e) {
System.out.println("Could not get typeface '" + assetPath + "' because " + e.getMessage());
return null;
}
}
return cache.get(assetPath);
}
}
}
}