2

I use StaticLayout to handle my text, then showed in TextView.But when i call getHeight of StaticLayout to get handled text height, it is less than the textview's true height. below is core code:


 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()

and this is log info:

staticLayoutHeight -> 237 
lineHeight -> 46 
textViewheight -> 277
textSize -> 39.0 
destiny -> 2.625
Alan W.
  • 4,566
  • 2
  • 15
  • 26

1 Answers1

0

do your mobile is HUAWEI? I find vivo is right, but HUAWEI need wrap

val heightByStaticLayout: Float = heightByStaticLayoutOrigin - (if (staticLayout.lineCount <= 1) 0f else ((staticLayout.lineCount - density) * density))
Michael Mao
  • 433
  • 5
  • 9