Android TextView部分字体变色或字体变大小
2015-12-28 15:49
471 查看
A.
SpannableStringBuilder style=new SpannableStringBuilder(str);
//SpannableStringBuilder实现CharSequence接口
style.setSpan(new ForegroundColorSpan(Color.RED), 0, 2,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE );
style.setSpan(new ForegroundColorSpan(Color.YELLOW), 2, 4,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE );
style.setSpan(new ForegroundColorSpan(Color.GREEN), 4, 6,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE );
tv.setText(style);//将其添加到tv中
B
![](https://img-blog.csdn.net/20151228155125325?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
B
SpannableStringBuilder style=new SpannableStringBuilder(str);
//SpannableStringBuilder实现CharSequence接口
style.setSpan(new ForegroundColorSpan(Color.RED), 0, 2,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE );
style.setSpan(new ForegroundColorSpan(Color.YELLOW), 2, 4,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE );
style.setSpan(new ForegroundColorSpan(Color.GREEN), 4, 6,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE );
tv.setText(style);//将其添加到tv中
B
String html = "预计收益<font color='#ff0000'>"+et_buy_sum.getText().toString()+"</font>元"; tv_profit_think.setText(Html.fromHtml(html));
B
TextView textView1 = (TextView) findViewById(R.id.textView1); TextView textView2 = (TextView) findViewById(R.id.textView2); TextView textView3 = (TextView) findViewById(R.id.textView3); TextView textView4 = (TextView) findViewById(R.id.textView4); //两次加大字体,设置字体为红色(big会加大字号,font可以定义颜色) textView1.setText(Html.fromHtml("北京市发布霾黄色预警,<font color='#ff0000'><big><big>外出携带好</big></big></font>口罩")); //设置字体大小为3级标题,设置字体为红色 textView2.setText(Html.fromHtml("北京市发布霾黄色预警,<h3><font color='#ff0000'>外出携带好</font></h3>口罩")); //设置字体大小为58(单位为物理像素),设置字体为红色,字体背景为黄色 textView3.setText("北京市发布霾黄色预警,外出携带好口罩"); Spannable span = new SpannableString(textView3.getText()); span.setSpan(new AbsoluteSizeSpan(58), 11, 16, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); span.setSpan(new ForegroundColorSpan(Color.RED), 11, 16, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); span.setSpan(new BackgroundColorSpan(Color.YELLOW), 11, 16, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); textView3.setText(span); //两次缩小字体,设置字体为红色(small可以减小字号) textView4.setText(Html.fromHtml("北京市发布霾黄色预警,<font color='#ff0000'><small><small>外出携带好</small></small></font>口罩"));
相关文章推荐
- Android桌面快捷方式那些事与那些坑
- Android原始XML的读写操作
- 编写android的widget
- Android编程开发之EditText实现输入QQ表情图像的方法
- android不太常用的控件
- 【Android】facebook api Exception: launchMode of the caller is singleInstance.
- Android实现Banner界面广告图片循环轮播(包括实现手动滑动循环)
- android获取手机内部存储空间和外部存储空间
- Android 开发环境下载地址 -- 百度网盘 adt-bundle android-studio sdk adt 下载
- Android编程开发之EditText中不输入特定字符会显示相关提示信息的方法
- Android 开源组件 ----- Android LoopView无限自动轮转控件
- Android开发笔记(三十)SQLite数据库基础操作
- Android uses-permission权限
- android studio gradle
- Android版添加phonegap--ionic时间选择插件插件教程
- Android四大组件之Activity详解
- Android实战之 4.4版本及以上透明状态栏,兼容5.0及以上
- android studio 极光推送 问题
- Class_file&&Android应用调用方法
- 任玉刚【Android开发艺术探索】读后笔记二