1)概述:
安卓上使用自定义的字体比较简单,在assets文件夹下面,自己定义一个font文件夹,然后,把自己的字体放进去,对相应的TextView设置自定义字体
Typeface typeface = Typeface.createFromAsset(instance.getAssets(), fonts/xxx.ttf);
textView.setTypeface(typeface);
2)使用:
第一步:在自定义的MyApplication中初始化字体调用方法
public class MyApplication extendsApplication {
private Typeface typeface;
private static MyApplication _instance;
@Override
public void onCreate() {
super.onCreate();
_instance = (MyApplication) getApplicationContext();
typeface = Typeface.createFromAsset(_instance.getAssets(), fonts/xxx.ttf);
}
public static MyApplication getInstace() {
return_instance;
}
public Typeface getTypeface() {
returntypeface;
}
public void setTypeface(Typeface typeface) {
this.typeface = typeface;
}
}
第二步:View对其引用:
public class MyTextView extendsTextView {
public MyTextView(Context context) {
super(context);
setTypeface(MyApplication.getInstace().getTypeface());
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
setTypeface(MyApplication.getInstace().getTypeface());
}
public MyTextView(Context context, AttributeSet attrs, intdefStyleAttr) {
super(context, attrs, defStyleAttr);
setTypeface(MyApplication.getInstace().getTypeface());
}
}
3)结语:
完毕!谢谢阅读