发表1 分钟读完 (大约178个字)
SpannableString 之居中显示 ImageSpan
自定义布局:SpannableString 之居中显示 ImageSpan
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| class CenteredImageSpan(context: Context, drawableRes: Int) : ImageSpan(context, drawableRes) { override fun draw( canvas: Canvas, text: CharSequence, start: Int, end: Int, x: Float, top: Int, y: Int, bottom: Int, paint: Paint ) { val b = drawable val fm = paint.fontMetricsInt var transY = ((y + fm.descent + y + fm.ascent) / 2 - b.bounds.bottom / 2) if (transY > bottom - b.bounds.bottom) transY = bottom - b.bounds.bottom canvas.save() canvas.translate(x, transY.toFloat()) b.draw(canvas) canvas.restore() } }
|
1 2 3 4 5 6 7 8 9 10 11
| val spanStr = SpannableStringBuilder() spanStr.append("# ") spanStr.append(title) val imageSpan = CenteredImageSpan(this, R.mipmap.ic_topic_detail_jinghao_black) spanStr.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
spanStr.append(" #") val len = spanStr.length val cornerSpan = CenteredImageSpan(this, R.mipmap.ic_topic_detail_remen) spanStr.setSpan(cornerSpan, len - 1, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) binding.ctTalkDetailInfo.talkNameTv.text = spanStr
|