正如标题所说,我想知道是否有可能在单个textview元素中实现两个不同颜色的字符。


当前回答

使用SpannableStringBuilder

SpannableStringBuilder builder = new SpannableStringBuilder();

SpannableString str1= new SpannableString("Text1");
str1.setSpan(new ForegroundColorSpan(Color.RED), 0, str1.length(), 0);
builder.append(str1);

SpannableString str2= new SpannableString(appMode.toString());
str2.setSpan(new ForegroundColorSpan(Color.GREEN), 0, str2.length(), 0);
builder.append(str2);

TextView tv = (TextView) view.findViewById(android.R.id.text1);
tv.setText( builder, TextView.BufferType.SPANNABLE);

其他回答

芬兰湾的科特林:

@JvmStatic
    @BindingAdapter(
        "app:txt1",
        "app:txt2",
        "app:color1",
        "app:color2",
        requireAll = false
    )
    fun setColors(
        txtView: AppCompatTextView,
        txt1: String,
        txt2: String,
        color1: Int,
        color2: Int
    ) {
        txtView.setColors(txt1 = txt1, txt2 = txt2, color1 = color1, color2)
    }


fun AppCompatTextView.setColors(txt1: String, txt2: String, color1: Int, color2: Int) {


        val word: Spannable = SpannableString(txt1)

        word.setSpan(
            ForegroundColorSpan(ContextCompat.getColor(this.context, color1)),
            0,
            word.length,
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
        )

        this.text = word
        val wordTwo: Spannable = SpannableString(txt2)

        wordTwo.setSpan(
            ForegroundColorSpan(ContextCompat.getColor(this.context, color2)),
            0,
            wordTwo.length,
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        this.append(wordTwo)

    }


<androidx.appcompat.widget.AppCompatTextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:txt1="@{}"
            app:txt2="@{}"
            app:color1="@{}"
            app:color2="@{}" />

自从API 24以来,你有FROM_HTML_OPTION_USE_CSS_COLORS,所以你可以在CSS中定义颜色,而不是一直重复使用font color=" 更清楚-当你有一些html,你想突出一些预定义的标签-你只需要在你的html顶部添加CSS片段

尽可能使用SpannableBuilder类而不是HTML格式,因为它比HTML格式解析更快。 在Github上查看我自己的基准测试“SpannableBuilder vs HTML” 谢谢!

是的,如果你用html的font-color属性格式化字符串,然后把它传递给方法html . fromhtml(你的文本在这里)

String text = "<font color=#cc0029>First Color</font> <font color=#ffcc00>Second Color</font>";
yourtextview.setText(Html.fromHtml(text));

我已经这么做了,试试吧:

TextView textView=(TextView)findViewById(R.id.yourTextView);//init

//here I am appending two string into my textView with two diff colors.
//I have done from fragment so I used here getActivity(), 
//If you are trying it from Activity then pass className.this or this; 

textView.append(TextViewUtils.getColoredString(getString(R.string.preString),ContextCompat.getColor(getActivity(),R.color.firstColor)));
textView.append(TextViewUtils.getColoredString(getString(R.string.postString),ContextCompat.getColor(getActivity(),R.color.secondColor)));

在你的TextViewUtils类中添加这个方法:

 /***
 *
 * @param mString this will setup to your textView
 * @param colorId  text will fill with this color.
 * @return string with color, it will append to textView.
 */
public static Spannable getColoredString(String mString, int colorId) {
    Spannable spannable = new SpannableString(mString);
    spannable.setSpan(new ForegroundColorSpan(colorId), 0, spannable.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    Log.d(TAG,spannable.toString());
    return spannable;
}