-2

アプリケーション全体でテキストのフォントを変更したいアプリケーションが 1 つあります。 プログラムで、またはマニフェストのxmlで
アプリケーションのフォントを変更する方法はありますか?

4

4 に答える 4

2

これを試して

1. ttf ファイルを assets フォルダーに配置し、これらの行を Java ファイルに追加します。

Typeface font = Typeface.createFromAsset(activity.getAssets(),"fonts/androidnation.ttf");

tv.setTypeface(font);

2.xmlで設定する

XML書体

于 2013-12-17T09:32:42.903 に答える
0

これを行う最も簡単な方法は、独自の TextView を作成することです。

public class MyTextView extends TextView {

    public DMTextView(Context context, AttributeSet attrs) {
      super(context, attrs);
      // you need your TypefaceFile "myTypeface.ttf" in the assets folder of your project
      setTypeface(Typeface.createFromAsset(context.getAssets(),"myTypeface.ttf"));
    }

    // add the other constructors if you want
}

これで、xml のどこでも使用できます: <com.your.package.MyTextView ... >通常のテキストビューと同じように

Typeface をキャッシュすることで改善される可能性があるため、TextView への参照ごとに再度作成する必要はありません。

于 2013-12-17T09:31:01.890 に答える
0

アセットにフォントフォルダーを作成し、貼り付けたいフォントを貼り付けます。

1 つのクラスを作成します。Typesafe.javaという名前を付けます

public enum TypeSafe {
   HELVETICANEUELTCOMBD,
   HELVETICANEUELTCOMBDCN,
   HELVETICANEUELTCOMCN,
}

その後、またはクラスActivityがある場合は、 1 つのメソッドを作成します。Utility

public void setTypeface(TextView textView, TypeSafe type, AssetManager assetManager){
    if (TypeSafe.HELVETICANEUELTCOMBD.equals(type)) {
        final Typeface typeface = Typeface.createFromAsset(assetManager, "fonts/HelveticaNeueLTCom-Bd.ttf");
        textView.setTypeface(typeface);
    } else if (TypeSafe.HELVETICANEUELTCOMBDCN.equals(type)) {
        final Typeface typeface1 = Typeface.createFromAsset(assetManager, "fonts/HelveticaNeueLTCom-BdCn.ttf");
        textView.setTypeface(typeface1);
    } 
}

でこれらのメソッドを呼び出しますactivity

setTypeface(yourtextView, TypeSafe.HELVETICANEUELTCOMLT, getAssets());
于 2013-12-17T09:41:37.593 に答える
0

フォントを fonts フォルダーに配置し、次のコードを使用します。

TextView tv = (TextView) findViewById(R.id.appname);
Typeface face = Typeface.createFromAsset(getAssets(),"fonts/epimodem.ttf");
tv.setTypeface(face);

これは、プログラムでフォントを設定する方法です。

于 2013-12-17T09:36:49.817 に答える