是否可以通过if条件为TextView中的不同文本设置多种颜色?
这是我的代码:
mColoredText = findViewById(R.id.questionText);
String mColoredString = "BLACK RED GREEN YELLOW ORANGE BLUE WHITE";
SpannableStringBuilder builder = new SpannableStringBuilder();
if(mColoredString.contains("RED")) {
String red = "RED";
SpannableString redSpannable = new SpannableString(red);
redSpannable.setSpan(new ForegroundColorSpan(Color.RED), 0, red.length(), 0);
builder.append(redSpannable);
}
if(mColoredString.contains("YELLOW")) {
String yellow = "YELLOW";
SpannableString whiteSpannable = new SpannableString(yellow);
whiteSpannable.setSpan(new ForegroundColorSpan(Color.YELLOW), 0, yellow.length(), 0);
builder.append(whiteSpannable);
}
if(mColoredString.contains("BLUE")) {
String blue = "BLUE";
SpannableString blueSpannable = new SpannableString(blue);
blueSpannable.setSpan(new ForegroundColorSpan(Color.BLUE), 0, blue.length(), 0);
builder.append(blueSpannable);
}
mColoredText.setText(builder, TextView.BufferType.SPANNABLE);
但是最终结果始终是打印出来的:红色,黄色和蓝色,只有三个文本。
我希望将黑色,红色,绿色,黄色,橙色,蓝色,白色全部书写在一起,如果没有可跨度的颜色,则适用白色。
答案 0 :(得分:0)
您创建一个新的构建器,并仅向其中添加红色和蓝色。这就是为什么您总是看到红黄蓝的原因。
如果您想更新原始文本,则必须首先从中创建一个SpannableString
。
SpannableString mColoredString = new SpannableString("BLACK RED GREEN YELLOW ORANGE BLUE WHITE");
if (mColoredString.toString().contains("RED")) {
mColoredString.setSpan(new ForegroundColorSpan(Color.RED), 6, 9, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
if (mColoredString.toString().contains("YELLOW")) {
mColoredString.setSpan(new ForegroundColorSpan(Color.YELLOW), 16, 22, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
if (mColoredString.toString().contains("BLUE")) {
mColoredString.setSpan(new ForegroundColorSpan(Color.BLUE), 30, 34, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
questionText.setText(mColoredString);
请阅读this article以获得详细说明。
编辑:
如果您不知道start
和end
的位置,则必须计算它们:
if (mColoredString.toString().contains("YELLOW")) {
int start = mColoredString.toString().indexOf("YELLOW");
int end = start + "YELLOW".length();
mColoredString.setSpan(new ForegroundColorSpan(Color.YELLOW), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
答案 1 :(得分:0)
尝试此代码。
textView = findViewById(R.id.tvData);
String mColoredString = "BLACK RED GREEN YELLOW ORANGE BLUE WHITE";
SpannableStringBuilder builder = new SpannableStringBuilder();
String strArray[] = mColoredString.split(" ");
for (int i = 0; i < strArray.length; i++) {
if (strArray[i].equals("RED")) {
SpannableString redSpannable = new SpannableString(strArray[i]);
redSpannable.setSpan(new ForegroundColorSpan(Color.RED), 0, strArray[i].length(), 0);
builder.append(redSpannable);
} else if (strArray[i].equals("YELLOW")) {
SpannableString whiteSpannable = new SpannableString(strArray[i]);
whiteSpannable.setSpan(new ForegroundColorSpan(Color.YELLOW), 0, strArray[i].length(), 0);
builder.append(whiteSpannable);
} else if (strArray[i].equals("BLUE")) {
SpannableString blueSpannable = new SpannableString(strArray[i]);
blueSpannable.setSpan(new ForegroundColorSpan(Color.BLUE), 0, strArray[i].length(), 0);
builder.append(blueSpannable);
} else {
builder.append(strArray[i]);
}
}
textView.setText(builder);
}