我这样使用它应该显示绿色但显示黑色
所有 rgb 值都是动态的(Web 服务)在浏览器中工作的相同代码(W3 学校)
mytextview.setText(Html
.fromHtml("<font style=\"color: rgb(102, 204, 0);\">REGISTRATION</font>"));
你在哪里写“注册”是绿色的?
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));
尝试这个
String formattedText = "This is <font color='#659D32'>green</font>";
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);
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);