我使用 StaticLayout 来处理我的文本,然后显示在 TextView 中。但是当我调用 StaticLayout 的 getHeight 来获取处理的文本高度时,它小于 textview 的真实高度。以下是核心代码:
private fun createStaticLayout(text: SpannableStringBuilder): StaticLayout {
val width = measuredWidth - paddingLeft - paddingRight
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
StaticLayout.Builder.obtain(text, 0, text.length, paint, width)
.setAlignment(Layout.Alignment.ALIGN_NORMAL)
.setIncludePad(includeFontPadding)
.setLineSpacing(lineSpacingExtra, lineSpacingMultiplier)
.build()
} else {
StaticLayout(
text,
paint,
width,
Layout.Alignment.ALIGN_NORMAL,
lineSpacingMultiplier,
lineSpacingExtra,
includeFontPadding
)
}
}
text = tempText2
layoutParams.height = tempLayout.height + paddingTop + paddingBottom
requestLayout()
这是日志信息:
staticLayoutHeight -> 237
lineHeight -> 46
textViewheight -> 277
textSize -> 39.0
destiny -> 2.625