如何在设置中指定字体文件而不是代码?

时间:2014-12-09 09:10:53

标签: android android-typeface

我正在尝试装饰我的应用程序字体。我可以从代码中做到这一点。但不知道如何在设置中这样做。我在代码中这样做

public class Fonts {
    public static Typeface getHeaderFont(Context context){
        return Typeface.createFromAsset(context.getAssets(), "header_levelI.ttf");
    }
    public static Typeface getSubHeaderFont(Context context){
        return Typeface.createFromAsset(context.getAssets(), "header_levelII.ttf");
    }
}

Typeface type= Fonts.getHeaderFont(getActivity());
        TextView header = (TextView) v.findViewById(R.id.cassaName);
        header.setTypeface(type);

设置android studio只有标准字体。如何在文件中指定不写额外代码的字体?

1 个答案:

答案 0 :(得分:0)

使用自定义字体创建自定义TextView。

注意:将所需的字体文件放在assets / fonts目录中非常重要。

public class CustomTextView extends TextView {
    public CustomTextView(Context context) {
        super(context);
        setFont();
    }
    public CustomTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setFont();
    }
    public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setFont();
    }

    private void setFont() {
        Typeface font = Typeface.createFromAsset(getContext().getAssets(),"fonts/ss-symbolicons-line.ttf");
        setTypeface(font, Typeface.NORMAL);
    }
}