【发布时间】:2021-08-17 09:52:35
【问题描述】:
我有这样的文字:
- 正在加载项目 1
- 正在加载项目 2
- 正在加载项目 3
我想更改每一行的颜色(或 fontWeight)以向用户显示进度。
【问题讨论】:
我有这样的文字:
我想更改每一行的颜色(或 fontWeight)以向用户显示进度。
【问题讨论】:
您可以为此使用SpannableString
val currentText = textView.text
val spannable = SpannableString(currentText)
spannable.setSpan(ForegroundColorSpan(Color.RED), 0, endOfFirstLine, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
spannable.setSpan(ForegroundColorSpan(Color.GREEN), endOfFirstLine, endOfSecondLine, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
spannable.setSpan(ForegroundColorSpan(Color.YELLOW), endOfSecondLine, endOfThirdLine, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
// Set the colored text
textView.text = spannable
【讨论】: