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

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

(来源:twimg.com)


当前回答

另一个答案将非常相似,但不需要设置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);

其他回答

First Part **Second Part should be Bold** last Part

此文本应该使用SpannableString进行更改

import android.graphics.Typeface.BOLD
import android.text.Spannable
import android.text.Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
import android.text.SpannableString
import android.text.style.BackgroundColorSpan
import android.text.style.ForegroundColorSpan
import android.text.style.StyleSpan

val firstPart = "First Part  "
val secondPart = "Second Part should be Bold"
val thirdPart = "  last Part"
val finalString = firstPart + secondPart + thirdPart

val sb: Spannable = SpannableString(finalString).also {
                // ... Change text Colour
                it.setSpan(
                    ForegroundColorSpan(getColor(requireContext(), R.color.pink)),
                    finalString.indexOf(secondPart),
                    finalString.indexOf(secondPart) + secondPart.length,
                    SPAN_EXCLUSIVE_EXCLUSIVE
                )
                // ... Make the text Bold
                it.setSpan(
                    StyleSpan(BOLD),
                    finalString.indexOf(secondPart),
                    finalString.indexOf(secondPart) + secondPart.length,
                    SPAN_EXCLUSIVE_EXCLUSIVE
                )
                // ... Change Background Colour
                it.setSpan(
                    BackgroundColorSpan(getColor(requireContext(), R.color.lightPink)),
                    finalString.indexOf(secondPart) - 1,
                    finalString.indexOf(secondPart) + secondPart.length + 1,
                    SPAN_EXCLUSIVE_EXCLUSIVE
                )
}

yourTextView.text = sb

从开发人员文档中,更改一个可伸缩对象的颜色和大小:

1-创建类:

    class RelativeSizeColorSpan(size: Float,@ColorInt private val color: Int): RelativeSizeSpan(size) {

    override fun updateDrawState(textPaint: TextPaint?) {
        super.updateDrawState(textPaint)
        textPaint?.color = color
    } 
}

2 .使用该类创建你的spannable:

    val spannable = SpannableStringBuilder(titleNames)
spannable.setSpan(
    RelativeSizeColorSpan(1.5f, Color.CYAN), // Increase size by 50%
    titleNames.length - microbe.name.length, // start
    titleNames.length, // end
    Spannable.SPAN_EXCLUSIVE_INCLUSIVE
)

这里有些答案不是最新的。因为,您将(在大多数情况下)在链接上添加一个自定义的clic操作。

此外,正如文档帮助所提供的,您的跨接字符串链接颜色将有一个默认颜色。 默认的链接颜色是主题的强调色或android:textColorLink(如果这个属性是在主题中定义的)。

这里有一种安全的方法。

 private class CustomClickableSpan extends ClickableSpan {

    private int color = -1;

    public CustomClickableSpan(){
        super();
        if(getContext() != null) {
            color = ContextCompat.getColor(getContext(), R.color.colorPrimaryDark);
        }
    }

    @Override
    public void updateDrawState(@NonNull TextPaint ds) {
        ds.setColor(color != -1 ? color : ds.linkColor);
        ds.setUnderlineText(true);
    }

    @Override
    public void onClick(@NonNull View widget) {
    }
}

然后使用它。

   String text = "my text with action";
    hideText= new SpannableString(text);
    hideText.setSpan(new CustomClickableSpan(){

        @Override
        public void onClick(@NonNull View widget) {
            // your action here !
        }

    }, 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    yourtextview.setText(hideText);
    // don't forget this ! or this will not work !
    yourtextview.setMovementMethod(LinkMovementMethod.getInstance());

希望这对你有很大帮助!

另一个答案将非常相似,但不需要设置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);

当我试图理解一个新概念时,我总是发现视觉例子很有帮助。

背景颜色

SpannableString spannableString = new SpannableString("Hello World!");
BackgroundColorSpan backgroundSpan = new BackgroundColorSpan(Color.YELLOW);
spannableString.setSpan(backgroundSpan, 0, spannableString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);

前景颜色

SpannableString spannableString = new SpannableString("Hello World!");
ForegroundColorSpan foregroundSpan = new ForegroundColorSpan(Color.RED);
spannableString.setSpan(foregroundSpan, 0, spannableString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);

结合

SpannableString spannableString = new SpannableString("Hello World!");
ForegroundColorSpan foregroundSpan = new ForegroundColorSpan(Color.RED);
BackgroundColorSpan backgroundSpan = new BackgroundColorSpan(Color.YELLOW);
spannableString.setSpan(foregroundSpan, 0, 8, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableString.setSpan(backgroundSpan, 3, spannableString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);

进一步的研究

解释SPAN_EXCLUSIVE_EXCLUSIVE等Span标志的含义 Android Spanned, SpannedString, Spannable, SpannableString和CharSequence