是否有可能在TextView中设置文本跨度的颜色?

我想做一些类似于Twitter应用程序的事情,其中一部分文本是蓝色的。见下图:

(来源:twimg.com)


当前回答

下面的内容非常适合我

    tvPrivacyPolicy = (TextView) findViewById(R.id.tvPrivacyPolicy);
    String originalText = (String)tvPrivacyPolicy.getText();
    int startPosition = 15;
    int endPosition = 31;

    SpannableString spannableStr = new SpannableString(originalText);
    UnderlineSpan underlineSpan = new UnderlineSpan();
    spannableStr.setSpan(underlineSpan, startPosition, endPosition, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

    ForegroundColorSpan backgroundColorSpan = new ForegroundColorSpan(Color.BLUE);
    spannableStr.setSpan(backgroundColorSpan, startPosition, endPosition, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

    StyleSpan styleSpanItalic  = new StyleSpan(Typeface.BOLD);

    spannableStr.setSpan(styleSpanItalic, startPosition, endPosition, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

    tvPrivacyPolicy.setText(spannableStr);

以上代码的输出

其他回答

create textview in ur layout paste this code in ur MainActivity TextView textview=(TextView)findViewById(R.id.textviewid); Spannable spannable=new SpannableString("Hello my name is sunil"); spannable.setSpan(new ForegroundColorSpan(Color.BLUE), 0, 5, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); textview.setText(spannable); //Note:- the 0,5 is the size of colour which u want to give the strring //0,5 means it give colour to starting from h and ending with space i.e.(hello), if you want to change size and colour u can easily

您可以在Kotlin中使用扩展函数

fun CharSequence.colorizeText(
    textPartToColorize: CharSequence,
    @ColorInt color: Int
): CharSequence = SpannableString(this).apply {
    val startIndexOfText = this.indexOf(textPartToColorize.toString())
    setSpan(ForegroundColorSpan(color), startIndexOfText, startIndexOfText.plus(textPartToColorize.length), 0)
}

用法:

val colorizedText = "this text will be colorized"
val myTextToColorize = "some text, $colorizedText continue normal text".colorizeText(colorizedText,ContextCompat.getColor(context, R.color.someColor))
viewBinding.myTextView.text = myTextToColorize

另一个答案将非常相似,但不需要设置TextView的文本两次

TextView TV = (TextView)findViewById(R.id.mytextview01);

Spannable wordtoSpan = new SpannableString("I know just how to whisper, And I know just how to cry,I know just where to find the answers");        

wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 15, 30, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

TV.setText(wordtoSpan);

我也有同样的问题。 @Dano的回答绝对正确。但这对我没用。 之后,我发现问题,我已经添加ClickableSpan。它会把我的颜色改成另一种颜色(强调色)

问题


当你在ForegroundColorSpan或UnderlineSpan后添加ClickableSpan时,SpannableStringBuilder将不会改变颜色和undline。

解决方案


1. 与ClickableSpan

你可以覆盖ClickableSpan内部的updateDrawState方法。 在updateDrawState方法中,您应该删除超级回调。 之后,你应该根据需要修改你的文本绘制。

2. 没有ClickableSpan

添加ForegroundColorSpan来改变文本颜色 Add UnderlineSpan在文本中添加下划线。

使用kotlin-ktx,您可以轻松实现这一点

        bindView?.oneTimePasswordTitle?.text = buildSpannedString {
        append("One Time Password ")
        inSpans(
            ForegroundColorSpan(ContextCompat.getColor(bindView?.oneTimePasswordTitle?.context!!,R.color.colorPrimaryText))
        ){
            append(" (As Registered Email)")
        }

    }