[英]How do I change the typeface of a textview in a widget?
I have a messaging widget that displays a list of the user's messages, and unread messages should appear as bold. 我有一个消息传递小部件,它显示用户消息的列表,未读消息应显示为粗体。
My code: 我的代码:
RemoteViews messageRow = new RemoteViews(mContext.getPackageName(), R.layout.row_message);
messageRow.setTextViewText(R.id.row_user_name, mMessages.get(position).getSender());
messageRow.setTextViewText(R.id.row_time, mMessages.get(position).getDate());
if (!mMessages.get(position).isIsRead()){
// Bold the textviews if the message is unread
}
I'm looking for something akin to textView.setTypeface(textView.getTypeface(), BOLD);
我正在寻找类似于
textView.setTypeface(textView.getTypeface(), BOLD);
that works on widget textviews. 适用于小部件textviews。 This syntax doesn't seem to exist for RemoteViews.
RemoteViews似乎不存在此语法。
Thanks! 谢谢!
Try like this . 尝试这样。
1.Use SpannableString
in your code . 1.在代码中使用
SpannableString
。
2.Then use new StyleSpan(android.graphics.Typeface.BOLD)
or new StyleSpan(android.graphics.Typeface.NORMAL)
. 2.然后使用
new StyleSpan(android.graphics.Typeface.BOLD)
或new StyleSpan(android.graphics.Typeface.NORMAL)
。
3.Set to your RemoteViews
3.设置为您的
RemoteViews
Example 例
// SpannableString init
SpannableString mspInt = null;
// the text you want to change the style
String text = "yourString";
// SpannableString init
mspInt = new SpannableString(text);
if (!mMessages.get(position).isIsRead()){
// Bold the textviews if the message is unread
// use setSpan to set BOLD to your text
mspInt.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
messageRow.setTextViewText(R.id.your_id, mspInt);
} else {
// use setSpan to set NORMAL to your text
mspInt.setSpan(new StyleSpan(Typeface.NORMAL), 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
messageRow.setTextViewText(R.id.your_id, mspInt);
}
Please try this , this works for me Just add "assistant_bold.ttf" file to your asset folder. 请尝试此操作,这对我有用。只需将“ assistant_bold.ttf”文件添加到资产文件夹中即可。 and then import android.graphics.Typeface to you class
然后将android.graphics.Typeface导入您的课程
Typeface xyz; 字体xyz;
xyz = Typeface.createFromAsset(context.getAssets(), "assistant_bold.ttf"); xyz = Typeface.createFromAsset(context.getAssets(),“ assistant_bold.ttf”); messageRow.setTypeface(xyz);
messageRow.setTypeface(xyz);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.