0

この投稿のおかげで、アクティビティにフォントを追加して、そのクラス内で使用できることがわかりました。使用:

 TextView hindiTextView=(TextView)findViewById(R.id.txtbx);
 Typeface hindiFont=Typeface.createFromAsset(getAssets(),"fonts/fontname.ttf");
 mytextView.setTypeface(hindiFont);

しかし、1 つの場所にフォントを追加して、それをアプリケーション全体で使用したい場合はどうすればよいでしょうか? (マニフェストまたは一部のプロパティ ファイルのように)

4

3 に答える 3

1

実際には、アプリケーション全体に共通のカスタム フォントを実装する方法はありません。ここでチェックアウトつまり 、いいえ、アプリケーション全体のフォントを設定することはできません。

それでも試してみたい場合は、以下のように参照してください

一般的なフォント クラス:

public final class FontsOverride {

    public static void setDefaultFont(Context context,
            String staticTypefaceFieldName, String fontAssetName) {
        final Typeface regular = Typeface.createFromAsset(context.getAssets(),
                fontAssetName);
        replaceFont(staticTypefaceFieldName, regular);
    }

    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();
        }
    }
}

次に、いくつかのデフォルト フォントをオーバーロードする必要があります。次に例を示します。

FontsOverride.setDefaultFont(this, "DEFAULT", "MyFontAsset.ttf");
FontsOverride.setDefaultFont(this, "MONOSPACE", "MyFontAsset2.ttf");
FontsOverride.setDefaultFont(this, "SANS_SERIF", "MyFontAsset3.ttf");

もちろん、同じフォント ファイルを使用している場合は、これを改善して一度だけ読み込むようにすることもできます。

于 2013-10-11T07:02:28.407 に答える
0

いいえ、申し訳ありません。作成したばかりのコードを使用してのみ、カスタム フォントを追加できます。

于 2013-10-11T06:33:00.373 に答える
0

TextView を拡張するカスタム クラスを作成し、フォント ロジックを追加します。layout で Android TextView を使用する代わりに、使用します <com.mypackage.myTextView android:id="@+id/myid />

于 2013-10-11T06:33:48.660 に答える