其实,安卓上使用自定义的字体非常得简单,在assets文件夹下面,自己定义一个font文件夹,然后,把自己的字体放进去,可以重命名一下,如图:
这样之后,在代码中,设置一下就可以,如下面所示:
Typeface typeface = Typeface.createFromAsset(_instance.getAssets(), "fonts/mi4.ttf"); textView.setTypeface(typeface);
但其实这样还有一些问题,我一个一个说:
而且,这样,也会有大量的代码重复。
这两个问题,我最后的解决办法如下:
首先:自定义applicaion,现在,一般都是这样自定义的application,然后,在程序如下:
public class MyApplication extends Application { private Typeface typeface; private static MyApplication _instance; @Override public void onCreate() { super.onCreate(); _instance = (MyApplication) getApplicationContext(); typeface = Typeface.createFromAsset(_instance.getAssets(), "fonts/mi4.ttf"); } public static MyApplication getInstace() { return _instance; } public Typeface getTypeface() { return typeface; } public void setTypeface(Typeface typeface) { this.typeface = typeface; } }
然后,使用自定义的View:
public class MyTextView extends TextView { 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, int defStyleAttr) { super(context, attrs, defStyleAttr); setTypeface(MyApplication.getInstace().getTypeface()); } }
这样,在所有的界面中,我们可以使用我们这个自定义的textView,我们测试并且软件现在已经上线了,完全没有问题。
其它的,如果editText,button等 ,同理。
版权声明:本文为博主原创文章,未经博主允许不得转载。
android 自定义控件字体,解决字体偏移,卡顿,代码重复等问题
原文地址:http://blog.csdn.net/carlos1992/article/details/46791827