简体   繁体   English

如何将 Drawable (ripple-drawable) 设置为 span 的背景?

[英]How to set a Drawable (ripple-drawable) as background of a span?

How can I set the background of a part of a textView to a drawable with a ripple effect?如何将 textView 的一部分的背景设置为具有波纹效果的可绘制对象?

spannableString.setSpan(
            BackgroundColorSpan(ANYCOLOR), startFocus, endFocus,
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
        )

This code can just set the background to a color, but not to a XML-File.此代码只能将背景设置为颜色,但不能设置为 XML 文件。 How to do that?怎么做? Thanks in advance!提前致谢!

You can use clickable span for that:您可以为此使用可点击范围:

fun getClickableSpan(action: (() -> Unit)): ClickableSpan {
        return object : ClickableSpan() {
            override fun onClick(widget: View) {
                action()
            }

            override fun updateDrawState(ds: TextPaint) {
                super.updateDrawState(ds)
                ds.isUnderlineText = false
            }
        }
    }

  text_view.apply {
            val spannable = getSpannableString(
               getClickableSpan {
                   action()
                },
                fullString,
                clikcableString
            )
            movementMethod = LinkMovementMethod.getInstance()
            text = spannable
        }

This code will provide you clickable part of text in TextView with ripple effect.此代码将为您提供 TextView 中具有波纹效果的可点击部分文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM