如何为newText“ep”着色
public void onClick(View v) {
String text = tw.getText().toString();
int n = 4;
String newText = text.substring(0, n) + "e" + text.substring(n + 1);
tw.setText(newText);
答案 0 :(得分:2)
您需要使用ForegroundColorSpan
final String text = "something something text";
final int n = 4;
final String newText = text.substring(0, n) + "e" + text.substring(n + 1);
final ForegroundColorSpan fcs = new ForegroundColorSpan(Color.RED);
final SpannableString ss = new SpannableString(newText);
ss.setSpan(fcs, n, n + 1, 0);
tv.setText(ss);
答案 1 :(得分:0)
是的,如果你使用html格式化String
您需要使用Html.fromHtml()在XML字符串中使用HTML。只需在布局XML中引用带有HTML的String就行了。
例如:
String text = "<font color=#cc0029>Exempl</font> <font color=#ffcc00>e</font>";
myTextView.setText(Html.fromHtml(text);