我有TextView
持有某个文字。是否可以在每个时间段内更改TextView
中每个单词的背景?例如,突出显示第一个单词,然后是第二个单词,然后是第三个单词,依此类推。
答案 0 :(得分:2)
是的,这是可能的。
String html = "hello <font color='#ff0000'>there</font>";
textView.setText(Html.fromHtml(html));
答案 1 :(得分:1)
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.my_text_view);
textView.setText("Hello there");
mMyRunnable.start = 0;
mMyRunnable.text = textView.getText().toString();
mHandler = new Handler();
mHandler.postDelayed(mMyRunnable, 1000);
}
private MyRunnable mMyRunnable = new MyRunnable();
private class MyRunnable implements Runnable{
String text;
int start = 0;
@Override
public void run () {
//get the next index of space..notice that this answer assumes that there are no double sapces
final int end = text.indexOf(32, start);
Spannable wordSpan = new SpannableString(text);
//case this is the last word
if(end == -1){
wordSpan.setSpan(new ForegroundColorSpan(Color.CYAN), start, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
//color the current word, set start end start handler again
else{
wordSpan.setSpan(new ForegroundColorSpan(Color.CYAN), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
start = end + 1;
mHandler.postDelayed(mMyRunnable, 1000);
}
textView.setText(wordSpan);
}
}
答案 2 :(得分:0)
是的,有可能。可以使用SpannableString作为TextView内容来实现。您将不得不深入了解文档以实现您想要的目标。在您的案例中使用的有用跨度之一是BackgroundColorSpan