Android Studio加载字体

1、展示效果:
这里写图片描述

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

}

猜你喜欢

转载自blog.csdn.net/pillar1066527881/article/details/79072192