开发者

android widget: set typeface programmatically

开发者 https://www.devze.com 2023-04-12 01:11 出处:网络
I understand that to use custom fonts in a widget I need to render the font to a bitmap, but how do I set a system typeface programmatically?

I understand that to use custom fonts in a widget I need to render the font to a bitmap, but how do I set a system typeface programmatically?

android:typeface="serif"

in XML is successful, but I cannot find a way to do this at run time.

I currently have

int fn = fullDate.length();
SpannableString dt = new SpannableString(ful开发者_如何学运维lDate);
dt.setSpan(new StyleSpan(Typeface.BOLD), 0, fn, 0);
remoteView.setTextViewText(R.id.dateText, dt);

Which works, but using, for example, Typeface.SERIF, gives The constructor StyleSpan(Typeface) is undefined


The constructor for StyleSpan that you're using takes in a constant defined in Typeface and not one of the fields. The constants available are

Typeface.BOLD
Typeface.BOLD_ITALIC
Typeface.ITALIC
Typeface.NORMAL

If you want to set the typeface to a specific font, you need to use TextView.setTypeFace()

EDIT This cannot be done for widgets since you only have access to the remote view. Others have circumvented this by passing an image to the remote view. See this question for details: How to use a custom typeface in a widget?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号