2015-11-25 102 views
0

我想使用Html.fromHtml()方法将颜色设置为部分文本。设置TextView的部分文字颜色?

我的代码是

String name = "<font color=\"#B034A2\">" + notificationListBean.getUsername() + "</font>" 
textview.setText(Html.fromHtml(name)); 

但文本颜色没有改变。

我也试过,但这个也没有工作

Spannable wordtoSpan = new SpannableString(name);   
wordtoSpan.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.noti_color)), 0, name.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 

textview.setText(wordtoSpan);

我应该怎么做什么样的变化?

+0

你确定'textview'显示在屏幕上吗? – Blackbelt

+0

是的,但颜色没有改变 –

+0

发表更多代码。 – Blackbelt

回答

1

昨天我面临同样的问题Html.fromHtml(),所以我已经使用spannable这里的字符串是如何,我会在下方修改

SpannableStringBuilder builder = new SpannableStringBuilder(); 

       SpannableString carOwnerSpannable= new SpannableString("Mayuri"); 
       carOwnerSpannable.setSpan(new ForegroundColorSpan(ContextCompat.getColor(JobDetailTabbedActivity.this,R.color.colorAccent)), 0, carOwnerName.length(), 0); 
       builder.append(carOwnerSpannable); 

       builder.append(" | LastName:Joshi"); 

       textView.settext(builder); 

评论你的代码,如果这行不通。

预期输出:

涅茧利(绿色(我的颜色口音为绿色))|名字:乔希(在白色)

添加<color name="colorAccent">#1AB394</color>到colors.xml

+0

Thnaks这个工程... –

1

尝试:

textView.setText(Html.fromHtml(name), TextView.BufferType.SPANNABLE); 
+0

我试过还没有工作 –

0
Spannable wordtoSpan = new SpannableString(name);   
wordtoSpan.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.noti_color)), 0, name.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
textview.setText(wordtoSpan); 

你仅仅使用应该工作这段代码。我把颜色加起来。用简单的颜色测试它&固定范围:

wordtoSpan.setSpan(new ForegroundColorSpan(Color.Red), 0, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
相关问题