【问题标题】:How to change color of a TextView line by line?如何逐行更改 TextView 的颜色?
【发布时间】:2021-08-17 09:52:35
【问题描述】:

我有这样的文字:

  • 正在加载项目 1
  • 正在加载项目 2
  • 正在加载项目 3

我想更改每一行的颜色(或 fontWeight)以向用户显示进度。

【问题讨论】:

标签: android kotlin textview


【解决方案1】:

您可以为此使用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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-17
    • 2016-02-19
    • 2015-07-22
    相关资源
    最近更新 更多