这个方法是从别人的项目里扣出来得。
至于性能什么的 没考虑过哈。
首先用到的类:(自己建一个就好)
public class Typefaces {
private static final String TAG = "Typefaces";
private static final Hashtable<String, Typeface> cache = new Hashtable<String, Typeface>();
public static Typeface get(Context c, String assetPath) {
synchronized (cache) {
if (!cache.containsKey(assetPath)) {
try {
Typeface t = Typeface.createFromAsset(c.getAssets(), assetPath);
cache.put(assetPath, t);
} catch (Exception e) {
Log.e(TAG, "Could not get typeface '" + assetPath + "' because " + e.getMessage());
return null;
}
}
return cache.get(assetPath);
}
}
}
然后再来看我们怎么使用它:
1.在工程的assets文件夹里放置我们要使用的字体;
2.在需要改变字体的地方这么用:获得控件,设置字体。
TextView tv = (TextView) findViewById(R.id.my_textview);
// set fancy typeface
tv.setTypeface(Typefaces.get(this, "Satisfy-Regular.ttf"));
就这么简单哈