StaticLayout height is less than TextView's true height

375 views Asked by At

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
1

There are 1 answers

1
Michael Mao On

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