我正在android studio上构建一个具有特定计算结果的应用程序,我需要在结果中用红色做一些注释,请参阅示例:
考虑你的考虑(r1,r2,r3)是字符串
result.setText ( "creat result" +
"\n salaz " + r1+
"\n jalf " +r2+
"\n mdar" +r3
);
结果:
salaz 20 mg
jalf 10 mg
mdar 5 mg
所以我需要jalf 10毫克红线
需要这一行红色"\n jalf " +r2+"
有没有办法让它变红?!!?
答案 0 :(得分:0)
我不确定这会起作用,但理论上应该这样。
Spannable r1Span = new SpannableString(r1);
Spannable r2Span = new SpannableString(r2);
Spannable r3Span = new SpannableString(r3);
r1Span.setSpan(new ForegroundColorSpan(Color.RED), 0, r1.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
r2Span.setSpan(new ForegroundColorSpan(Color.RED), 0, r2.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
r3Span.setSpan(new ForegroundColorSpan(Color.RED), 0, r3.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
String result = TextUtils.concat("creat result" +
"\n salaz " + r1Span+
"\n jalf " +r2Span+
"\n mdar" +r3Span);
result.setText (result);
让我知道它是怎么回事。
答案 1 :(得分:0)
尝试让我知道它是否有效:
YOUR_TEXTVIEW.setText(Html.fromHtml("<font color=\"#006400\">" + "Price: $" + _price + " " + "</font>" + " " + "<font color=\"#3F23F5\">" + "Rooms: " + _room+ " " + "</font>"
+ " " + "<font color=\"#DF7401\">" + "District: " + _district + "</font>"));
答案 2 :(得分:-1)
使用
int colorRed = getResources().getColor(R.color.red);
String text = getString(R.string.text);
SpannableString spannable = new SpannableString(text); // here we set the color
spannable.setSpan(new ForegroundColorSpan(colorBlue), 0, text.length(), 0);
yourTextView.setText(spannable);
将此处的长度处理到您想要设置颜色的位置