解决方案就是生成一张自定义字体的图片,Android12为了安全废弃了小组件自定义字体,请使用setImagebitmap替代。
/**
* Creates and returns a new bitmap containing the given text.
*/
public static Bitmap createTextBitmap(final String text, final Typeface typeface, final float textSizePixels, final int textColour)
{
final TextPaint textPaint = new TextPaint();
textPaint.setTypeface(typeface);
textPaint.setTextSize(textSizePixels);
textPaint.setAntiAlias(true);
textPaint.setSubpixelText(true);
textPaint.setColor(textColour);
textPaint.setTextAlign(Paint.Align.LEFT);
Bitmap myBitmap = Bitmap.createBitmap((int) textPaint.measureText(text), (int) textSizePixels, Bitmap.Config.ARGB_8888);
Canvas myCanvas = new Canvas(myBitmap);
myCanvas.drawText(text, 0, myBitmap.getHeight(), textPaint);
return myBitmap;
}