简体   繁体   中英

how to set Custom font for Widget?

i want to set custom font for TextView in widget,How?

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

Just replace xyz.ttf with your chosen font.

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;
}

The above code applies the font and text to image view.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM