自定义textview使用自定义字体

Cre*_*ale 1 android android-widget drawtext ondraw typeface

我正在尝试使用自己的自定义字体实现自定义textview.

有没有办法在做Super.onDraw()之前设置字体?

以便将常用字体替换为我想要使用的自定义字体.

就像是:

protected void onDraw(Canvas canvas)
{
    Typeface font1 = Typeface.createFromAsset(context.getAssets(), "fonts/myfonts.ttf");
    this.setTypeface(font1);
    this.setTextSize(18);
    super.onDraw(canvas);
}
Run Code Online (Sandbox Code Playgroud)

我知道上面的代码不起作用.

或者我别无选择,只能使用drawText()来做到这一点?

小智 9

每次调用onDraw方法时,创建新的Typeface对象是一种非常糟糕的做法.字体设置等应该在类构造函数中完成,但不是每次绘制视图时都要完成.