Android的BackgroundColorSpan在lineSpacing小于1时出现问题
创始人
2024-10-07 03:32:55
0

使用自定义的LineHeightSpan替代lineSpacing小于1时的LineSpacingSpan。

示例代码如下:

class CustomLineHeightSpan(private val height: Int) : LineHeightSpan {

    override fun chooseHeight(
        text: CharSequence?,
        start: Int,
        end: Int,
        spanstartv: Int,
        v: Int,
        fm: Paint.FontMetricsInt?
    ) {
        if (fm != null) {
            val lineHeight = fm.bottom - fm.top
            if (lineHeight < height) {
                val delta = (height - lineHeight) / 2
                fm.top -= delta
                fm.ascent -= delta
                fm.bottom += delta
                fm.descent += delta
            }
        }
    }
}

// 可以在代码中设置TextView的行高
textView.setLineSpacing(0f, 1.5f)

// 在设置 SpannableString 时使用自定义的 LineHeightSpan
val spannableString = SpannableString("This is a text with background color")
val backgroundColorSpan = BackgroundColorSpan(Color.RED)
val lineHeightSpan =
    CustomLineHeightSpan((textView.textSize * (1 + 1.5f)).toInt())
spannableString.setSpan(
    backgroundColorSpan,
    5,
    10,
    Spanned.SPAN_INCLUSIVE_EXCLUSIVE
)
spannableString.setSpan(
    lineHeightSpan,
    5,
    10,
    Spanned.SPAN_INCLUSIVE_EXCLUSIVE
)

相关内容

热门资讯

玩家必看科普!wejoker辅... 玩家必看科普!wejoker辅助软件价格,wepoker底牌透视脚本,力荐教程(有挂技巧)是由北京得...
透视辅助!pokemomo辅助... 透视辅助!pokemomo辅助工具(透视)一贯有挂(详细辅助必胜教程);1、很好的工具软件,可以解锁...
热点推荐!wepoker-h5... 热点推荐!wepoker-h5下载,wejoker辅助软件价格,可靠教程(有挂教程);科技安装教程;...
透视新版!wpk插件(透视)确... 透视新版!wpk插件(透视)确实真的是有挂(详细辅助wepoke教程)1、机器人多个强度级别选择2、...
今日百科!wepoker辅助器... 1、今日百科!wepoker辅助器下载,wepoker透视器免费,解密教程(有挂神器)。2、wepo...
透视挂透视!德普之星透视辅助软... 透视挂透视!德普之星透视辅助软件激活码(透视)都是是真的有挂(详细辅助黑科技教程)在进入德普之星透视...
一分钟揭秘!we poker辅... 一分钟揭秘!we poker辅助器v3.3,aapoker透视脚本入口,教你教程(有挂教程)1、点击...
透视ai代打!破解辅助插件we... 透视ai代打!破解辅助插件wepoker(透视)其实是有挂(详细辅助线上教程);辅助器中分为三种模型...
解密关于!werplan外挂,... 解密关于!werplan外挂,wepoker可以透视码,详细教程(有挂攻略);致您一封信;亲爱wep...
透视了解!impoker辅助(... 透视了解!impoker辅助(透视)确实是真的有挂(详细辅助wepoke教程)在进入辅助挂后,参与本...