17

私はbuttonAndroidウィジェットを使用して作成しました。ボタンテキストのフォントをに設定したいHelv Neue 67 Med Cond。このフォントを取得してAndroidレイアウトファイルのボタンテキストに設定するにはどうすればよいですか?

4

7 に答える 7

25

I guess you may have already found the answer, but if not (and for other developers), you can do it like this:

1.you want to save the "Helv Neue 67 Med Cond.ttf" in to assets folder. then

For TextView

  TextView txt = (TextView) findViewById(R.id.custom_font);
  Typeface typeface = Typeface.createFromAsset(getAssets(), "Helv Neue 67 Med Cond.ttf");
  txt.setTypeface(typeface);

For Button

  Button n=(Button) findViewById(R.id.button1);
  Typeface typeface = Typeface.createFromAsset(getAssets(), "Helv Neue 67 Med Cond.ttf");
  n.setText("show");
  n.setTypeface(typeface);
于 2013-08-15T10:55:51.087 に答える
17

まず、ttfファイルをassetsフォルダーに配置する必要があります。次に、次のコードを使用して、Buttonの場合と同じように、TextViewでカスタムフォントを設定できます。

TextView txt = (TextView) findViewById(R.id.custom_font);
Typeface font = Typeface.createFromAsset(getAssets(), "Helv Neue 67 Med Cond.ttf");
txt.setTypeface(font);
于 2011-06-16T13:37:58.110 に答える
5

If you plan to add the same font to several buttons I suggest that you go all the way and implement subclass button:

public class ButtonPlus extends Button {

    public ButtonPlus(Context context) {
        super(context);
        applyCustomFont(context);
    }

    public ButtonPlus(Context context, AttributeSet attrs) {
        super(context, attrs);
        applyCustomFont(context);
    }

    public ButtonPlus(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        applyCustomFont(context);
    }

    private void applyCustomFont(Context context) {
            Typeface customFont = FontCache.getTypeface("fonts/candy.ttf", context);
            setTypeface(customFont);
        }
    }

And here's the FontCache to reduce memory usage on older devices:

public class FontCache {

    private static Hashtable<String, Typeface> fontCache = new Hashtable<>();

    public static Typeface getTypeface(String name, Context context) {
        Typeface tf = fontCache.get(name);
        if(tf == null) {
            try {
                tf = Typeface.createFromAsset(context.getAssets(), name);
            }
            catch (Exception e) {
                return null;
            }
            fontCache.put(name, tf);
        }
        return tf;
    }
}

And finally an example use in a layout:

 <com.my.package.buttons.ButtonPlus
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_sometext"/>

This may seem like an awful lot of work, but you'll thank me once you have couple of handfuls of buttons and textfields that you want to change font on.

Also you can see this tutorial and a example in GitHub.

于 2016-02-05T11:51:04.387 に答える
2
于 2011-06-16T13:47:43.273 に答える
1

次を使用できます。

android:typeface="yourfont"
于 2011-06-16T13:17:13.283 に答える
0

Helv Neue 67 Med Condフォントをダウンロードしてアセットフォルダに保存する必要があります。ダウンロードしたフォントをmyfont.ttf

次のコードを使用してフォントを設定します

Typeface tf = Typeface.createFromAsset(getAssets(), "myfont.ttf");
        TextView TextViewWelcome = (TextView)findViewById(R.id.textViewWelcome);
        TextViewWelcome.setTypeface(tf);

ありがとうDeepak

于 2011-06-16T13:47:42.060 に答える
0

This is a good article, I used it several times and works: http://mobile.tutsplus.com/tutorials/android/customize-android-fonts/

于 2011-06-16T14:18:10.067 に答える