如何设置小部件的自定义字体?

时间:2018-08-15 05:22:32

标签: java android fonts widget typeface

我想在小部件中为TextView设置自定义字体,怎么办?

 RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.weather_widget);
views.setTextViewText(R.id.txv_widget_location, "my text");

1 个答案:

答案 0 :(得分:0)

只需将xyz.ttf替换为您选择的字体即可。

public Bitmap createBitmap(String texttoshow) 
{
  Bitmap myBitmap = Bitmap.createBitmap(160, 84, Bitmap.Config.ARGB_8888);
  Canvas myCanvas = new Canvas(myBitmap);
  Paint paint = new Paint();
  Typeface weatherwidget = Typeface.createFromAsset(this.getAssets(),"xyz.ttf");
  paint.setAntiAlias(true);
  paint.setSubpixelText(true);
  paint.setTypeface(weatherwidget);
  paint.setStyle(Paint.Style.FILL);
  paint.setColor(Color.WHITE);
  paint.setTextSize(65);
  paint.setTextAlign(Align.CENTER);
  myCanvas.drawText(texttoshow, 80, 60, paint);
  return myBitmap;
}

上面的代码将字体和文本应用于图像视图。

RemoteViews views = new RemoteViews(context.getPackageName(), 
 R.layout.weather_widget);
views.setImageViewBitmap(R.id.txv_widget_location, createBitmap("my_text"));