-1

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

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

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

3 回答 3

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));
于 2014-12-03T11:37:50.553 回答
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);
于 2014-12-03T11:38:11.413 回答
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);
于 2014-12-03T11:41:42.040 回答