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"));就这么简单哈
至于性能什么的 没考虑过哈。
首先用到的类:(自己建一个就好)
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"));就这么简单哈
相关文章推荐
- Android 消息处理源码分析(1)
- Android开机自动启动程序
- qt android录音程序添加 Android 权限
- Android Studio 快捷键
- Android多点触控技术实战,自由地对图片进行缩放和移动
- Android 从照相机和相册选择图片使用原生代码裁剪(支持4.4 KIKAT及以上机型)
- Android 图片轮番 继承ViewGroup实现 可以直接使用
- android控件
- Android瀑布流照片墙实现,体验不规则排列的美感
- Activity的绘制流程简单分析(基于android 4.0源码进行分析)
- 【Android】状态栏通知Notification、NotificationManager详解
- Android了解四大基本组件
- Android Studio开发JNI工程
- android studio中空文件夹是否折叠
- Android照片墙应用实现,再多的图片也不怕崩溃
- Android开发规范
- Android Bitmap详细介绍
- Android高效加载大图、多图解决方案,有效避免程序OOM
- 提升Android应用开发性能的十大要点
- android开发下的一些技术要点