2

我正在尝试使用自定义字体它可以在没有问题的模拟器上运行。

但在 Smsung Galaxy Tab 上抛出以下错误:无法制作原生字体

这是我的代码:

               public static Typeface typeface;
             // -----define typeface

    typeface = Typeface.createFromAsset(getAssets(), "fonts/Verdana.TTf");
    Typeface.class.getField("DEFAULT").setAccessible(true);
                          ---------------------
        lblBrandCategory1.setTypeface(GuestActivity.typeface, 4);


            anyone knows the solution???
4

2 回答 2

2

我有这个(碰巧在 Galaxy Tab 上)几乎完全按照你正在做的事情做。结果对我来说是一个区分大小写的问题,例如文件名都是小写的,我在 java 代码中将 .ttf 文件名大写。

所以大概这意味着每当找不到 ttf 时都会出现此错误(因此请检查您的路径是否良好)。

于 2011-10-30T05:04:32.327 回答
0

我有同样的问题,我不相信它依赖于设备。

我通过确保以下内容解决了这个问题:

  1. 如果您有多个项目,请确保您的字体文件存储在主项目的资产文件夹中- 而不是依赖项目。

  2. 为了安全起见,请将您的字体重命名为全部小写并在您的代码中引用它。

    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);
        }
    }
}
}
于 2014-05-20T17:06:26.503 回答