在 Android 中设置 TextView span 的颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3282940/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:28:21  来源:igfitidea点击:

Set color of TextView span in Android

androidtextformattingtextview

提问by hpique

Is it possible to set the color of just span of text in a TextView?

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

I would like to do something similar to the Twitter app, in which a part of the text is blue. See image below:

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

alt text
(source: twimg.com)

替代文字
(来源:twimg.com

回答by DanO

Another answer would be very similar, but wouldn't need to set the text of the TextViewtwice

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

回答by mach

Here is a little help function. Great for when you have multiple languages!

这是一个小帮助功能。非常适合您有多种语言!

private void setColor(TextView view, String fulltext, String subtext, int color) {
    view.setText(fulltext, TextView.BufferType.SPANNABLE);
    Spannable str = (Spannable) view.getText();
    int i = fulltext.indexOf(subtext);
    str.setSpan(new ForegroundColorSpan(color), i, i + subtext.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}

回答by Suragch

I always find visual examples helpful when trying to understand a new concept.

在尝试理解新概念时,我总是发现视觉示例很有帮助。

Background Color

背景颜色

enter image description here

在此处输入图片说明

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);

Foreground Color

前景色

enter image description here

在此处输入图片说明

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);

Combination

组合

enter image description here

在此处输入图片说明

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);

Further Study

进一步研究

回答by Tiago

If you want more control, you might want to check the TextPaintclass. Here is how to use it:

如果你想要更多的控制,你可能想要检查TextPaint类。以下是如何使用它:

final ClickableSpan clickableSpan = new ClickableSpan() {
    @Override
    public void onClick(final View textView) {
        //Your onClick code here
    }

    @Override
    public void updateDrawState(final TextPaint textPaint) {
        textPaint.setColor(yourContext.getResources().getColor(R.color.orange));
        textPaint.setUnderlineText(true);
    }
};

回答by Jorgesys

Set your TextView′s text spannable and define a ForegroundColorSpanfor your text.

设置您TextView的文本可跨度并ForegroundColorSpan为您的文本定义一个。

TextView textView = (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);    
textView.setText(wordtoSpan);

回答by Dave

Another way that could be used in some situations is to set the link color in the properties of the view that is taking the Spannable.

在某些情况下可以使用的另一种方法是在采用 Spannable 的视图的属性中设置链接颜色。

If your Spannable is going to be used in a TextView, for example, you can set the link color in the XML like this:

例如,如果您的 Spannable 将在 TextView 中使用,您可以在 XML 中设置链接颜色,如下所示:

<TextView
    android:id="@+id/myTextView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColorLink="@color/your_color"
</TextView>

You can also set it in the code with:

您还可以在代码中设置它:

TextView tv = (TextView) findViewById(R.id.myTextView);
tv.setLinkTextColor(your_color);

回答by Thomas Emil Hansen

There's a factory for creating the Spannable, and avoid the cast, like this:

有一个用于创建 Spannable 的工厂,并避免强制转换,如下所示:

Spannable span = Spannable.Factory.getInstance().newSpannable("text");

回答by Hiren Patel

Set Coloron Textby passing Stringand color:

通过传递 Stringcolor文本设置颜色

private String getColoredSpanned(String text, String color) {
  String input = "<font color=" + color + ">" + text + "</font>";
  return input;
}

Set texton TextView / Button / EditTextetc by calling below code:

通过调用以下代码在TextView / Button / EditText等上设置文本

TextView:

文本视图:

TextView txtView = (TextView)findViewById(R.id.txtView);

Get Colored String:

获取彩色字符串:

String name = getColoredSpanned("Hiren", "#800000");

Set Text on TextView:

在 TextView 上设置文本:

txtView.setText(Html.fromHtml(name));

Done

完毕

回答by Ahamadullah Saikat

String text = "I don't like Hasina.";
textView.setText(spannableString(text, 8, 14));

private SpannableString spannableString(String text, int start, int end) {
    SpannableString spannableString = new SpannableString(text);
    ColorStateList redColor = new ColorStateList(new int[][]{new int[]{}}, new int[]{0xffa10901});
    TextAppearanceSpan highlightSpan = new TextAppearanceSpan(null, Typeface.BOLD, -1, redColor, null);

    spannableString.setSpan(highlightSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    spannableString.setSpan(new BackgroundColorSpan(0xFFFCFF48), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    spannableString.setSpan(new RelativeSizeSpan(1.5f), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    return spannableString;
}

Output:

输出:

enter image description here

在此处输入图片说明

回答by Antonio Vinicius Menezes Medei

Just to add to the accepted answer, as all the answers seem to talk about android.graphics.Coloronly: what if the color I want is defined in res/values/colors.xml?

只是添加到已接受的答案中,因为所有答案似乎android.graphics.Color都只讨论:如果我想要的颜色在res/values/colors.xml?

For example, consider Material Design colorsdefined in colors.xml:

例如,考虑以下定义的Material Design 颜色colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="md_blue_500">#2196F3</color>
</resources>

(android_material_design_colours.xmlis your best friend)

android_material_design_colours.xml是你最好的朋友)

Then use ContextCompat.getColor(getContext(), R.color.md_blue_500)where you would use Color.BLUE, so that:

然后使用ContextCompat.getColor(getContext(), R.color.md_blue_500)您会使用的地方Color.BLUE,以便:

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

becomes:

变成:

wordtoSpan.setSpan(new ForegroundColorSpan(ContextCompat.getColor(getContext(), R.color.md_blue_500)), 15, 30, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

Where I found that:

我发现的地方: