【发布时间】:2019-07-23 23:29:13
【问题描述】:
我正在尝试将一个单词移动到 android textview 中的下一个单词上方,就像附加的图像 (example image) 中一样。我已经设法使用 spannablestringbuilder 将单词向上移动(如上标),但我找不到将文本的右侧部分向左移动以填补空白的方法。有谁知道如何做到这一点?
这是我目前写的函数:
/**
* Adds clickable spans for words that are contained between "[" and "]"
*
* @param imString The string on which to apply clickable spans
*/
private fun addClickablePart(imString: String): SpannableStringBuilder
{
var string = imString
val spannableStringBuilder = SpannableStringBuilder((string.replace("[", "")).replace("]", ""))
var startIndex = string.indexOf("[")
while (startIndex != -1)
{
string = string.replaceFirst("[", "")
val endIndex = string.indexOf("]", startIndex)
string = string.replaceFirst("]", "")
val clickString = string.substring(startIndex, endIndex)
spannableStringBuilder.setSpan(
object: ClickableSpan()
{
override fun onClick(view: View)
{
HelperFunction.showToast(this@SongActivity, clickString)
}
override fun updateDrawState(text: TextPaint)
{
super.updateDrawState(text)
text.isUnderlineText = false
text.color = ContextCompat.getColor(this@SongActivity, R.color.colorAccent)
text.textSize = HelperFunction.spToPx(this@SongActivity, 12).toFloat()
text.baselineShift += (text.ascent()).toInt() // move chord upwards
text.typeface = Typeface.create(ResourcesCompat.getFont(this@SongActivity, R.font.roboto_mono), Typeface.BOLD) // set text to bold
}
},
startIndex, endIndex, 0)
startIndex = string.indexOf("[", endIndex)
}
return spannableStringBuilder
}
【问题讨论】:
-
屏幕截图显示了您已完成的操作或您的预期?
-
您好,欢迎来到 SO,请阅读 How do I ask a good question? 并提供代码。否则你的问题可能会被否决而得不到回答。
-
你可能应该在 html 中定义它,然后将 textview 内容设置为 html
-
您好! @VirRajpurohit 屏幕截图显示了我的预期。我提供了迄今为止编写的函数来处理字符串,并更新了图像以更清晰。
-
你好@Frieder!我已经提供了我编写的用于处理字符串的函数。
标签: android kotlin textview spannablestringbuilder