Android Textview HTML不显示颜色?

时间:2014-12-03 11:34:31

标签: android html textview fromhtml

我这样使用它应该显示绿色但显示黑色

所有rgb值都是动态的(Web服务) 在浏览器中工作的相同代码(W3 SCHOOLS)

mytextview.setText(Html
            .fromHtml("<font style=\"color: rgb(102, 204, 0);\">REGISTRATION</font>"));

3 个答案:

答案 0 :(得分:1)

你在哪里写“注册”是绿色的?

String hex = String.format("#%02x%02x%02x", r, g, b);
String html = String.format("<font color='%s'>REGISTRATION</font>",hex);

mytextview.setText(Html.fromHtml(html));

答案 1 :(得分:1)

试试这个

String formattedText = "This is &lt;font color='#659D32'&gt;green&lt;/font&gt;";
textElement.setText(Html.fromHtml(formattedText));

或者另一种方法是使用SpannableStringBuilder

  String registration = "REGISTRATION";
  SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(registration);
  ForegroundColorSpan colorSpannable = new ForegroundColorSpan(Color.rgb(102, 204, 0)); 
  spannableStringBuilder.setSpan(colorSpannable, 0, registration.length()-1, Spannable.SPAN_INCLUSIVE_INCLUSIVE); 
  mytextview.setText(spannableStringBuilder);

答案 2 :(得分:0)

mytextview.setText(Html.fromHtml("<![CDATA[<font color='#659D32'>REGISTRATION</font>]]>"));

或者使用SpannableStringBuilder:

String text = "REGISTRATION";
SpannableStringBuilder span = new SpannableStringBuilder(text);
span.setSpan(new ForegroundColorSpan(Color.parseColor("#659D32")), 0,  text.length(), SpannableStringBuilder.SPAN_EXCLUSIVE_EXCLUSIVE);
mytextview.setText(span);