Android Studio加载字体

1、展示效果:
Android Studio加载字体_第1张图片

2、字体文件放置位置

Android Studio加载字体_第2张图片

3、字体加载方法一: mTextView.setTypeface(Typefaces.get(this, “fonts/Satisfy-Regular.ttf”));
字体加载方法二: mTextView.setTypeface(Typeface.createFromAsset(getAssets(), “fonts/DIN_Alternate.ttf”));
字体加载方法三:
MDFontsUtils.setOcticons(mTextView);
自定义工具类:
public class MDFontsUtils {
public static Typeface OCTICONS;

/**
 * Get octicons typeface
 *
 * @param context
 * @return octicons typeface
 */
public static Typeface getOcticons(final Context context) {
    if (OCTICONS == null)
        OCTICONS = getTypeface(context, "fonts/Roboto-BoldCondensed.ttf");
    return OCTICONS;
}

/**
 * Set octicons typeface on given text view(s)
 *
 * @param textViews
 */
public static void setOcticons(final TextView... textViews) {
    if (textViews == null || textViews.length == 0)
        return;

    Typeface typeface = getOcticons(textViews[0].getContext());
    for (TextView textView : textViews)
        textView.setTypeface(typeface);
}

/**
 * Get typeface with name
 *
 * @param context
 * @param name
 * @return typeface
 */
public static Typeface getTypeface(final Context context, final String name) {
    return Typeface.createFromAsset(context.getAssets(), name);
}

}

你可能感兴趣的:(Android)