Android:如何在应用程序小部件中设置自定义字体?

Android:如何在应用程序小部件中设置自定义字体?,android,fonts,android-appwidget,Android,Fonts,Android Appwidget,你好,谢谢你的帮助 我需要在我的应用程序小部件中使用我的自定义字体 通常,我会使用类似于: TextView txt = (TextView) findViewById(R.id.custom_font); Typeface font = Typeface.createFromAsset(getAssets(), "myfont.ttf"); txt.setTypeface(font); 但在中,无法在RemoteView上调用setTypeface(字体) 有什么建议吗 谢谢 你不能

你好,谢谢你的帮助

我需要在我的应用程序小部件中使用我的自定义字体

通常,我会使用类似于:

TextView txt = (TextView) findViewById(R.id.custom_font);  
Typeface font = Typeface.createFromAsset(getAssets(), "myfont.ttf");  
txt.setTypeface(font);
但在中,无法在RemoteView上调用setTypeface(字体)

有什么建议吗


谢谢

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


(我知道这个用户很久以前就发布了,但是这个答案可能对其他人有用)

你可以使用以下方法

将字体渲染到画布上,然后将其传递到位图并将其指定给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));
希望这会有所帮助:)