如何使用 2 spannable 字符串的样式?
How to use styles for 2 spannable string?
我创建了一个变量,其中使用 SpannableStringBuilder
添加了 2 行。
在我为每个字符串使用 span 之后。
但不幸的是,样式应用不正确。
我的立场有问题,我只需要为 item.statusDecription
使用红色(例如:2020 年 6 月 26 日,12:54)
可能是什么问题?
val typeface = ResourcesCompat.getFont(listItemView.context, R.font.roboto_bold)
val consentDescription = SpannableStringBuilder(item.consentDescription)
val statusDescription = SpannableStringBuilder(item.statusDescription)
val spannable = SpannableStringBuilder("$consentDescription $statusDescription")
spannable.apply {
setSpan(StyleSpan(typeface!!.style), 0, item.consentDescription.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
setSpan(
ForegroundColorSpan(
ContextCompat.getColor(
listItemView.context,
R.color.grey_100
)
), 0, item.consentDescription.length, Spannable.SPAN_EXCLUSIVE_INCLUSIVE
)
setSpan(StyleSpan(typeface.style), item.statusDescription.indexOf(item.statusDescription, 0), item.statusDescription.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
setSpan(
ForegroundColorSpan(
ContextCompat.getColor(
listItemView.context,
R.color.red
)
), item.statusDescription.indexOf(item.statusDescription, 0), item.statusDescription.length, Spannable.SPAN_EXCLUSIVE_INCLUSIVE
)
}
subTitleView.text = spannable
试试这个方法
val description ="Sprint Bank Example"
val status ="5 26 Jun 2020, 12:54"
val typeface = ResourcesCompat.getFont(this, R.font.calibri)
val spannableDescription = SpannableString(description)
val spannableStatus = SpannableString(status)
val spannableBuilder = SpannableStringBuilder().apply {
spannableDescription.setSpan(
ForegroundColorSpan(context.getColorCompat(R.color.gray10)), 0, spannableDescription.length,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
)
spannableDescription.setSpan(typeface, 0, description.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
append(spannableDescription)
spannableStatus.setSpan(
ForegroundColorSpan(context.getColorCompat(R.color.red)), 0, spannableStatus.length,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
)
spannableStatus.setSpan(typeface, 0, status.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
append("\n")
append(spannableStatus)
}
subTitleView.text = spannableBuilder
输出:
我创建了一个变量,其中使用 SpannableStringBuilder
添加了 2 行。
在我为每个字符串使用 span 之后。
但不幸的是,样式应用不正确。
我的立场有问题,我只需要为 item.statusDecription
使用红色(例如:2020 年 6 月 26 日,12:54)
可能是什么问题?
val typeface = ResourcesCompat.getFont(listItemView.context, R.font.roboto_bold)
val consentDescription = SpannableStringBuilder(item.consentDescription)
val statusDescription = SpannableStringBuilder(item.statusDescription)
val spannable = SpannableStringBuilder("$consentDescription $statusDescription")
spannable.apply {
setSpan(StyleSpan(typeface!!.style), 0, item.consentDescription.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
setSpan(
ForegroundColorSpan(
ContextCompat.getColor(
listItemView.context,
R.color.grey_100
)
), 0, item.consentDescription.length, Spannable.SPAN_EXCLUSIVE_INCLUSIVE
)
setSpan(StyleSpan(typeface.style), item.statusDescription.indexOf(item.statusDescription, 0), item.statusDescription.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
setSpan(
ForegroundColorSpan(
ContextCompat.getColor(
listItemView.context,
R.color.red
)
), item.statusDescription.indexOf(item.statusDescription, 0), item.statusDescription.length, Spannable.SPAN_EXCLUSIVE_INCLUSIVE
)
}
subTitleView.text = spannable
试试这个方法
val description ="Sprint Bank Example"
val status ="5 26 Jun 2020, 12:54"
val typeface = ResourcesCompat.getFont(this, R.font.calibri)
val spannableDescription = SpannableString(description)
val spannableStatus = SpannableString(status)
val spannableBuilder = SpannableStringBuilder().apply {
spannableDescription.setSpan(
ForegroundColorSpan(context.getColorCompat(R.color.gray10)), 0, spannableDescription.length,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
)
spannableDescription.setSpan(typeface, 0, description.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
append(spannableDescription)
spannableStatus.setSpan(
ForegroundColorSpan(context.getColorCompat(R.color.red)), 0, spannableStatus.length,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
)
spannableStatus.setSpan(typeface, 0, status.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
append("\n")
append(spannableStatus)
}
subTitleView.text = spannableBuilder
输出: