昨天工作中偶然发现某款app的字体样式不是android自带的字体样式,看着很舒服。研究了一下发现还挺简单的。
1.从网上搜索一下字体格式文件(.ttf格式,百度上有很多)。下载,放置到eclipse中的assets目录下(注意.ttf文件名不能为中文)
2.自定义一个View继承Textview
public class MyFontTextView extends TextView {
public MyFontTextView(Context context) {
super(context);
init(context);
// TODO Auto-generated constructor stub
}
public MyFontTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
// TODO Auto-generated constructor stub
}
public MyFontTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
// TODO Auto-generated constructor stub
}
private void init(Context context) {
// TODO Auto-generated method stub
AssetManager aManager=context.getAssets();
Typeface font=Typeface.createFromAsset(aManager, "font/huawenxingkai.ttf");
setTypeface(font);
}
}
当MyFontTextView初始化的时候调用init()。在init()中执行Typeface的样式的替换
3.在布局文件中调用MyFontTextView(就行调用Textview一样)
4.在activity中初始化MyFontTextView,也完全可以将MyFontTextView当做textview来用即可