准备
自定义实现一个显示指定字体的 TextView
很简单,正好做项目遇到就记录下。 首先准备好相应的字体文件,ttf
格式的,放入assets
文件夹下,也可以在assets
目录下自定义目录放入。
实现
下面就是实现代码:
public class NumberTextView extends TextView {
public NumberTextView(Context context) {
this(context, null);
}
public NumberTextView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
Typeface typeface = Typeface.createFromAsset(CommonApplication.getInstance().getAssets(), "fonts/DINCondensedC-2.ttf");
setTypeface(typeface);
}
}
使用
在布局文件中和 TextView
一样使用就可以了。