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. 複数のプロジェクトがある場合は、フォント ファイルが依存プロジェクトではなく、メイン プロジェクトの assets フォルダーに保存されていることを確認してください。

  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 に答える