标签:
1、 必须事先在assets底下创建一fonts文件夹 并放入要使用的字体文件(.ttf)
2、写一个manager
/**
* 设置字体
*
*/
public class FontManager {
static String fongUrl = "fonts/fzltxh.ttf";
static Typeface tf;
public static void applyFont(Context context, ViewGroup root, String fontName) {
Typeface tf = getFont(context);
for (int i = 0; i < root.getChildCount(); i++) {
View v = root.getChildAt(i);
if (v instanceof TextView) {
((TextView) v).setTypeface(tf);
} else if (v instanceof Button) {
((Button) v).setTypeface(tf);
} else if (v instanceof EditText) {
((EditText) v).setTypeface(tf);
} else if (v instanceof ViewGroup) {
applyFont(context, (ViewGroup) v, fontName);
}
}
}
public static Typeface getFont(Context context) {
// if (tf == null) {
// tf = Typeface.createFromAsset(context.getAssets(), fongUrl);
// }
// return tf;
return Typeface.DEFAULT;
}
}
3、重写TextView
/**
* 自定义TextView
*
*/
public class MyTextView extends TextView {
public MyTextView(Context context) {
super(context);
init(context);
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
private void init(Context context) {
Typeface setFont = FontManager.getFont(context);
if (setFont != null) {
setTypeface(setFont);
}
}
}
4、在xml中直接用MyTextView,呆着全路径,直接替换TextView
另一种textView.setTypeface(FontManager.getFont());
标签:
原文地址:http://www.cnblogs.com/xunzhi/p/4496945.html