Android中的一个TextView中的字体设置不同大小


如图,这个是桌面Widget中的截图,最好是通过一个TextView实现,这是我提出的问题,近几天解决。呵呵,当然写两个TextView很简单也很容易设置。

Java代码

title.setText("Your big island <b>ADVENTURE!</b>");//这是原样显示,我想让加粗  

还有,我想能不能类似的给上边那样通过html标签设置样式。网上搜过,果然可以。

Java代码

  1. {   
  2.    final SpannableStringBuilder sb = new SpannableStringBuilder("your text here");   
  3.    final ForegroundColorSpan fcs = new ForegroundColorSpan(Color.rgb(158158158)); // Span to set text color to some RGB value   
  4.    final StyleSpan bss = new StyleSpan(Android.graphics.Typeface.BOLD); // Span to make text bold   
  5.    sb.setSpan(fcs, 04, Spannable.SPAN_INCLUSIVE_INCLUSIVE); // Set the text color for first 4 characters   
  6.    sb.setSpan(bss, 04, Spannable.SPAN_INCLUSIVE_INCLUSIVE); // make them also bold   
  7.    yourTextView.setText(sb);   
  8. }  

另外android帮我们封装了简单的方法。

Java代码

但是,我通过这个方法,在widget中设置,不太可行。又是个疑问了,但到在widget中通过html代码设置字体大小不行么?

如果你经常的使用设置TextView中的字体,最好使用Spannable或SpannableBuilder之类的。网上说Html.fromHtml不是很高效,因为这是通过一个很大的xml来解析的。

我再试试Spannable

Java代码

  1. TextView TV = (TextView)findViewById(R.id.mytextview01);    
  2. 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");           
  3. WordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 1530, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);   
  4. TV.setText(WordtoSpan); 
  • 1
  • 2
  • 下一页

相关内容