您的位置:首页 > 移动开发 > Android开发

android 更换字体

2015-07-02 17:01 447 查看
这个方法是从别人的项目里扣出来得。

至于性能什么的 没考虑过哈。

首先用到的类:(自己建一个就好)

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"));就这么简单哈
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: