gpt4 book ai didi

Android:更改 Spannable String 中换行符的高度不起作用

转载 作者:行者123 更新时间:2023-11-30 00:19:05 26 4
gpt4 key购买 nike

实际上,我正在尝试增加 Spannable 字符串中文本之间的行间距。然后我想通过增加换行符的跨度大小来实现。但似乎第二条新线没有受到影响。

起初我使用“a”作为分隔符来表明代码有效。

private fun getCenterSpannableText() : SpannableString {
val currency = "Currency"
val total = "Total"
val subtitle = "Subtitle"
val delimiter = "\n"
val finalStr = "$currency$delimiter$total$delimiter$subtitle"

val currencyIndex = finalStr.indexOf(currency)
val totalIndex = finalStr.indexOf(total)
val subtitleIndex = finalStr.indexOf(subtitle)

val s = SpannableString(finalStr)
s.setSpan(RelativeSizeSpan(1.4f), currencyIndex, currency.length, 0)
s.setSpan(ForegroundColorSpan(ContextUtil.instance.getColor(R.color.colorGrey5)), currencyIndex, currency.length, 0)

s.setSpan(RelativeSizeSpan(1.8f), totalIndex, totalIndex + total.length, 0)
s.setSpan(ForegroundColorSpan(ContextUtil.instance.getColor(R.color.colorGrey5)), totalIndex, totalIndex + total.length, 0)

s.setSpan(RelativeSizeSpan(1f), subtitleIndex, subtitleIndex + subtitle.length, 0)
s.setSpan(ForegroundColorSpan(ContextUtil.instance.getColor(R.color.colorGrey2)), subtitleIndex, subtitleIndex + subtitle.length, 0)

//make the \n have bigger fonts
s.setSpan(RelativeSizeSpan(2.5f), currencyIndex + currency.length, currencyIndex + currency.length + delimiter.length, 0)
s.setSpan(RelativeSizeSpan(2.5f), totalIndex + total.length , totalIndex + total.length + delimiter.length, 0)

return s
}

输出是这样的:

enter image description here

然后我将分隔符改为“\n”,它看起来不对:

enter image description here

我在 API24 模拟器上运行它

最佳答案

我认为发生的事情是这样的:

Currency\n
Total\n
Subtitle

在这种情况下你实际上想要这个:

Currency\n
Total\n
Subtitle\n

编辑:这意味着您可能想尝试将代码更改为

val finalStr = "$currency$delimiter$total$delimiter$subtitle$delimiter"

关于Android:更改 Spannable String 中换行符的高度不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46642837/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com