博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[转]Android中的一个TextView中的字体设置不同大小
阅读量:6903 次
发布时间:2019-06-27

本文共 4189 字,大约阅读时间需要 13 分钟。

本文转自:

 

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

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

还有,我想能不能类似的给上边那样通过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. }  
{   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帮我们封装了简单的方法。

Java代码  
  1. title.setText(Html.fromHtml("Your big island <b>ADVENTURE!</b>"));   
title.setText(Html.fromHtml("Your big island ADVENTURE!"));

但是,我通过这个方法,在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);  
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中的字体大小。

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);  
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);

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

 

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);  
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方法。

效果如图:

 

 

转载地址:http://blpdl.baihongyu.com/

你可能感兴趣的文章
Codeforces Round #345 (Div. 1) A - Watchmen 容斥
查看>>
在storyboard中的静态UITableView中拖入 UISearchBar and Search Display Controller出现的奇怪问题...
查看>>
Android消息推送完美解决方案全析
查看>>
除湿方法
查看>>
J2EE中getParameter与getAttribute以及EL表达式${requestScope}和${param[]}
查看>>
传递给数据库 'master' 中的日志扫描操作的日志扫描号无效
查看>>
Viewing Network Usage Data
查看>>
HTML5 移动端的上下左右滑动问题
查看>>
SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)
查看>>
Visual Studio示例代码浏览器
查看>>
MySQL中多表删除方法(转载)
查看>>
[Javascript] Fetch API
查看>>
[改善Java代码]不使用stop方法停止线程
查看>>
Spring Boot新模块devtools
查看>>
关于Android读取不同位置(drawable,asset,SDCard)的图片资源的总结
查看>>
HAProxy的三种不同类型配置方案
查看>>
Unity3d之截图
查看>>
Windows下的Jdk 1.7*安装并配置(图文详解)
查看>>
JS判断客户端是手机还是PC的2个代码(转)
查看>>
分布式服务框架设计和实现
查看>>