zoukankan      html  css  js  c++  java
  • [转]Android中的一个TextView中的字体设置不同大小

    本文转自:http://txlong-onz.iteye.com/blog/1142781


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

    Java代码 复制代码 收藏代码
    1. 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代码 复制代码 收藏代码
    1. title.setText(Html.fromHtml("Your big island <b>ADVENTURE!</b>"));   

    但是,我通过这个方法,在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);  

    经过测试通过这种方法还是不能够改变widget中的字体大小。

    Java代码 复制代码 收藏代码
    1. Spannable WordtoSpan = new SpannableString("大字小字");   
    2. WordtoSpan.setSpan(new AbsoluteSizeSpan(11), 01, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);   
    3. WordtoSpan.setSpan(new AbsoluteSizeSpan(21), 23, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);   
    4. remoteViews.setCharSequence(R.id.text11, "setText", WordtoSpan.toString());   
    5. ComponentName com = new ComponentName("com.jftt.widget""com.jftt.widget.MyWidgetProvider");   
    6. appWidgetManager.updateAppWidget(com, remoteViews);  

    经过我的修改终于成功了,原来试试自己使用错误啊。下边是正确代码。

    Java代码 复制代码 收藏代码
    1. Spannable WordtoSpan = new SpannableString("大字小字");   
    2. WordtoSpan.setSpan(new AbsoluteSizeSpan(20), 02, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);   
    3. WordtoSpan.setSpan(new AbsoluteSizeSpan(14), 24, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);   
    4. remoteViews.setCharSequence(R.id.text11, "setText", WordtoSpan);   
    5. ComponentName com = new ComponentName("com.jftt.widget""com.jftt.widget.MyWidgetProvider");   
    6. appWidgetManager.updateAppWidget(com, remoteViews);  

    相信对比可以看出我错在哪里了吧!是的,我错误的使用了toString方法。

    效果如图:

  • 相关阅读:
    设计模式——设计原则与思想总结
    SQL——性能优化篇(下)
    计算机组成原理——入门篇
    SQL——性能优化篇(中)
    SQL——性能优化篇(上)
    设计模式——规范与重构(下)
    设计模式——规范与重构(上)
    编译原理——实现一门脚本语言 应用篇
    编译原理——实现一门脚本语言 原理篇(下)
    设计模式——设计原则实战
  • 原文地址:https://www.cnblogs.com/freeliver54/p/2491491.html
Copyright © 2011-2022 走看看