---
title: "在AppWidget上使用自定义Typeface"
author: "dkmeteor"
date: "2012-02-10T11:31:06Z"
tags: []
categories: ["移动开发"]
slug: "appwidget-typeface"
draft: false
lastmod: "2012-02-10T11:31:06Z"
id: 1401741
url: "https://dkmeteor.iteye.com/blog/1401741"
---

在AppWidget上使用自定义Typeface

 

 

工作中需要在AppWidget上显示一些特殊字符，需要用到自己的 字体配置文件

 

Appwidget 的 RemoteViews并不提供API方法，无法设置TextView的Typeface属性。

这里用了些歪门邪道。

 

 

Typeface someFont = Typeface.createFromAsset(context.getAssets(), "Dr.eye\_phonetic\_symbol.ttf");

/\**

 * 构建音标图片

 \*/

Bitmap bitmap=Bitmap.createBitmap(300,30,Bitmap.Config.ARGB\_8888);

Canvas canvas=new Canvas(bitmap);

Paint paint=new Paint();

paint.setTypeface(someFont);

paint.setTextSize(14);

paint.setARGB(255, 147, 122, 74);

canvas.drawText(soundmark, 0, 20, paint);

canvas.save();

 

调用底层，直接把这个文字绘成图片

然后调用 setImageViewBitmap方法将其显示在AppWidget上

 

views.setImageViewBitmap(R.id.appwidget\_soundmark, bitmap);