0

您好,感谢您的帮助。

我需要在我的 App Widget 中使用我的自定义字体。

通常我会使用类似的东西:

TextView txt = (TextView) findViewById(R.id.custom_font);  
Typeface font = Typeface.createFromAsset(getAssets(), "myfont.ttf");  
txt.setTypeface(font);

但是 In 不能在 RemoteViews 上调用setTypeface(font) 。

请问有什么建议吗?

谢谢!

4

2 回答 2

3

您可以使用以下方法。

将字体渲染到画布上,然后将其传递给位图并将其分配给 ImageView。

public Bitmap buildUpdate(String text) 
{
    Bitmap myBitmap = Bitmap.createBitmap(10, 10, Bitmap.Config.ARGB_4444);
    Canvas myCanvas = new Canvas(myBitmap);
    Paint paint = new Paint();
    Typeface mytypeface = Typeface.createFromAsset(this.getAssets(),"fontname.ttf");
    paint.setAntiAlias(true);
    paint.setSubpixelText(true);
    paint.setTypeface(clock);
    paint.setStyle(Paint.Style.FILL);
    paint.setColor(Color.WHITE);
    paint.setTextSize(65);
    paint.setTextAlign(Align.CENTER);
    myCanvas.drawText(text, 80, 60, paint);
    return myBitmap;
}

像这样使用它:

String text = "This is my text";
RemoteViews views = new RemoteViews(getPackageName(), R.layout.my_widget_layout);
views.setImageViewBitmap(R.id.my+imageview, buildUpdate(text));

希望这会有所帮助:)

于 2014-03-21T15:42:02.393 回答
-1

您不能这样做,因为应用程序在不同的进程中运行,因此您只能使用系统字体。

(我知道用户很久以前就发布过这个,但答案可能对遇到它的其他人有用)

于 2013-09-27T12:03:55.487 回答