最近app完工,却要引入第三方字体,那改的叫蛋疼啊!日TMD客户。
下面是方法(有更好的方法欢迎指点)
/**
* 全局变量*
*/
@SuppressLint("SdCardPath")
public class MyApplication extends Application {
/**
* app字体
*/
public static Typeface TEXT_TYPE ;
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
try{
TEXT_TYPE = Typeface.createFromAsset(getAssets(),"YaHei.ttf");
}catch(Exception e){
TEXT_TYPE = null ;
}
}
}
public class MyTextView extends TextView {
public MyTextView(Context context) {
super(context);
setTypeface();
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
setTypeface();
}
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setTypeface();
}
private void setTypeface(){
// 如果自定义typeface初始化失败,就用原生的typeface
if(MyApplication.TEXT_TYPE == null){
setTypeface(getTypeface()) ;
}else{
setTypeface(MyApplication.TEXT_TYPE) ;
}
}
}
至于MyTextView要怎么使用,大家都懂,哈哈