您的位置:首页 > 移动开发 > Android开发

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中 



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>口罩"));
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: