如图,这个是桌面Widget中的截图,最好是通过一个TextView实现,这是我提出的问题,近几天解决。呵呵,当然写两个TextView很简单也很容易设置。
title.setText("Your big island ADVENTURE!");//这是原样显示,我想让加粗
还有,我想能不能类似的给上边那样通过html标签设置样式。网上搜过,果然可以。
{ final SpannableStringBuilder sb = new SpannableStringBuilder("your text here"); final ForegroundColorSpan fcs = new ForegroundColorSpan(Color.rgb(158, 158, 158)); // Span to set text color to some RGB value final StyleSpan bss = new StyleSpan(android.graphics.Typeface.BOLD); // Span to make text bold sb.setSpan(fcs, 0, 4, Spannable.SPAN_INCLUSIVE_INCLUSIVE); // Set the text color for first 4 characters sb.setSpan(bss, 0, 4, Spannable.SPAN_INCLUSIVE_INCLUSIVE); // make them also bold yourTextView.setText(sb); }
另外android帮我们封装了简单的方法。
title.setText(Html.fromHtml("Your big island ADVENTURE!"));
但是,我通过这个方法,在widget中设置,不太可行。又是个疑问了,但到在widget中通过html代码设置字体大小不行么?
如果你经常的使用设置TextView中的字体,最好使用Spannable或SpannableBuilder之类的。网上说Html.fromHtml不是很高效,因为这是通过一个很大的xml来解析的。
我再试试Spannable
TextView TV = (TextView)findViewById(R.id.mytextview01); Spannable WordtoSpan = new SpannableString("I know just how to whisper, And I know just how to cry,I know just where to find the answers"); WordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 15, 30, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); TV.setText(WordtoSpan);
经过测试通过这种方法还是不能够改变widget中的字体大小。
Spannable WordtoSpan = new SpannableString("大字小字"); WordtoSpan.setSpan(new AbsoluteSizeSpan(11), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); WordtoSpan.setSpan(new AbsoluteSizeSpan(21), 2, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); remoteViews.setCharSequence(R.id.text11, "setText", WordtoSpan.toString()); ComponentName com = new ComponentName("com.jftt.widget", "com.jftt.widget.MyWidgetProvider"); appWidgetManager.updateAppWidget(com, remoteViews);
经过我的修改终于成功了,原来试试自己使用错误啊。下边是正确代码。
Spannable WordtoSpan = new SpannableString("大字小字"); WordtoSpan.setSpan(new AbsoluteSizeSpan(20), 0, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); WordtoSpan.setSpan(new AbsoluteSizeSpan(14), 2, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); remoteViews.setCharSequence(R.id.text11, "setText", WordtoSpan); ComponentName com = new ComponentName("com.jftt.widget", "com.jftt.widget.MyWidgetProvider"); appWidgetManager.updateAppWidget(com, remoteViews);
相信对比可以看出我错在哪里了吧!是的,我错误的使用了toString方法。
效果如图: