嗨,我正在尝试将 textview 中的部分文本更改为某种颜色,但我无法实现它我尝试了 fromHtml、spannable和不同的方法,但它仍然无法正常工作。
我的尝试如下:
尝试1
Spannable wordtoSpan = Spannable.Factory.getInstance().newSpannable("text 6 Months(180 days) or 8,000 kilometers.");
wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 5, 15, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(wordtoSpan);
尝试2
String str = "@Second @Service:6 Months(180 days) or 8,000 kilometers.";
String[] str_array = str .split(" ");
boolean isExists = false;
for (int i = 0; i < str_array.length; i++) {
for (int j = 0; j < i; j++) {
if (str_array[j].equals(str_array[i])) {
isExists = true;
}
}
if (str_array[i].startsWith("@") && !isExists) {
str = str .replace(str_array[i],
"<font color='#8A0A0A'>" + str_array[i]
+ "</font>");
} else if (str_array[i].contains("#")) {
str = str .replaceAll(str_array[i],
"<font color='#000000'><b>" + str_array[i]
+ "</b></font>");
}
}
tv.setText(Html.fromHtml(str));
}
尝试3
myTextView.setText(Html.fromHtml(text + "<font color=white>" + CepVizyon.getPhoneCode() + "</font><br><br>"
+ getText(R.string.currentversion) + CepVizyon.getLicenseText()));
尝试4
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//method1
TextView tv = (TextView) findViewById(R.id.service);
tv.setText(Html.fromHtml("<font color=\"#FFFFFF\">" + "give your string here long long longlong" + "</font>" + "<font color=\"#47a842\"></font>"));
//method 2
//SpannableStringBuilder WordtoSpan = new SpannableStringBuilder("give your string here long long longlong");
//WordtoSpan.setSpan(new BackgroundColorSpan(Color.YELLOW),0,5,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//tv.setText(WordtoSpan);
}
非常感谢任何帮助。谢谢