如何更改TextView的一部分颜色?

时间:2022-01-04 06:50:06
text = text + CepVizyon.getPhoneCode() + "\n\n"
            + getText(R.string.currentversion) + CepVizyon.getLicenseText();
    activationText.setText(text);   
myTextView.setText(text);

I want to change color for CepVizyon.getPhoneCode()'s string. How can I do this?

我想改变CepVizyon.getPhoneCode()的字符串的颜色。我怎样才能做到这一点?

7 个解决方案

#1


108  

Spannable is more flexible:

Spannable更灵活:

String text2 = text + CepVizyon.getPhoneCode() + "\n\n"
            + getText(R.string.currentversion) + CepVizyon.getLicenseText();

Spannable spannable = new SpannableString(text2);

spannable.setSpan(new ForegroundColorSpan(Color.WHITE), text.length(), (text + CepVizyon.getPhoneCode()).length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

myTextView.setText(spannable, TextView.BufferType.SPANNABLE);

#2


55  

myTextView.setText(Html.fromHtml(text + "<font color=white>" + CepVizyon.getPhoneCode() + "</font><br><br>"
            + getText(R.string.currentversion) + CepVizyon.getLicenseText()));

#3


15  

If you have static text that needs color, you can add it without any code via the strings file:

如果你有需要颜色的静态文本,你可以通过strings文件添加它而不需要任何代码:

<string name="already_have_an_account">Already have an account? <font color='#01C6DB'>Login</font></string>

then

然后

<TextView
    android:layout_width="wrap_content"
    android:layout_height="64dp"
    android:text="@string/already_have_an_account"/>

result

结果

如何更改TextView的一部分颜色?

not sure which api versions this works on, but doesnt work for api 19 that ive tested so far, so probably only some of the most recent api versions support this

不确定这个api版本是如何工作的,但是到目前为止我测试过的api 19都不起作用,所以可能只有一些最新的api版本支持这个

edit: as @hairraisin mentioned in the comments, try using fgcolor instead of color for the font color, then it should work for lower api levels, but need more testing to be sure

编辑:正如@hairraisin在评论中提到的,尝试使用fgcolor而不是颜色作为字体颜色,那么它应该适用于较低的api级别,但需要更多测试才能确定

#4


13  

With regards to Maneesh's answer, this will work but you need to add and escape the quotes for the color attribute.

关于Maneesh的答案,这将有效,但您需要添加和转义color属性的引号。

myTextView.setText(Html.fromHtml(text + "<font color=\"#FFFFFF\">" + CepVizyon.getPhoneCode() + "</font><br><br>"
            + getText(R.string.currentversion) + CepVizyon.getLicenseText()));

#5


5  

It is good for me!

这对我有好处!

            Spannable spannable = new SpannableString("ABC In-Network DEF");
            String str = spannable.toString();
            iStart = str.indexOf("In-Network");
            iEnd = iStart + 10;/*10 characters = in-network. */

            SpannableString ssText = new SpannableString(spannable);
            ClickableSpan clickableSpan = new ClickableSpan() {
                @Override
                public void onClick(View widget) {
                    //your code at here.
                }

                @Override
                public void updateDrawState(TextPaint ds) {
                    super.updateDrawState(ds);
                    ds.setUnderlineText(true);
                    ds.setColor(getResources().getColor(R.color.green));
                }
            };
            ssText.setSpan(clickableSpan, iStart, iEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            mTextView.setText(ssText);
            mTextView.setMovementMethod(LinkMovementMethod.getInstance());
            mTextView.setHighlightColor(Color.TRANSPARENT);
            mTextView.setEnabled(true);

#6


0  

Here's a colorize function based on andyboot's answer:

这是基于andyboot的答案的colorize函数:

 /**
 * Colorize a specific substring in a string for TextView. Use it like this: <pre>
 * textView.setText(
 *     Strings.colorized("The some words are black some are the default.","black", Color.BLACK),
 *     TextView.BufferType.SPANNABLE
 * );
 * </pre>
 * @param text Text that contains a substring to colorize
 * @param word The substring to colorize
 * @param argb The color
 * @return the Spannable for TextView's consumption
 */
public static Spannable colorized(final String text, final String word, final int argb) {
    final Spannable spannable = new SpannableString(text);
    int substringStart=0;
    int start;
    while((start=text.indexOf(word,substringStart))>=0){
        spannable.setSpan(
                new ForegroundColorSpan(argb),start,start+word.length(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
        );
        substringStart = start+word.length();
    }
    return spannable;
}

#7


-4  

One way is to split myTextView to few separate TextViews, one of which would be only for phone code. Then controlling color of this specific TextView is pretty straight-forward.

一种方法是将myTextView拆分为几个单独的TextView,其中一个仅用于电话代码。然后控制这个特定TextView的颜色非常简单。

#1


108  

Spannable is more flexible:

Spannable更灵活:

String text2 = text + CepVizyon.getPhoneCode() + "\n\n"
            + getText(R.string.currentversion) + CepVizyon.getLicenseText();

Spannable spannable = new SpannableString(text2);

spannable.setSpan(new ForegroundColorSpan(Color.WHITE), text.length(), (text + CepVizyon.getPhoneCode()).length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

myTextView.setText(spannable, TextView.BufferType.SPANNABLE);

#2


55  

myTextView.setText(Html.fromHtml(text + "<font color=white>" + CepVizyon.getPhoneCode() + "</font><br><br>"
            + getText(R.string.currentversion) + CepVizyon.getLicenseText()));

#3


15  

If you have static text that needs color, you can add it without any code via the strings file:

如果你有需要颜色的静态文本,你可以通过strings文件添加它而不需要任何代码:

<string name="already_have_an_account">Already have an account? <font color='#01C6DB'>Login</font></string>

then

然后

<TextView
    android:layout_width="wrap_content"
    android:layout_height="64dp"
    android:text="@string/already_have_an_account"/>

result

结果

如何更改TextView的一部分颜色?

not sure which api versions this works on, but doesnt work for api 19 that ive tested so far, so probably only some of the most recent api versions support this

不确定这个api版本是如何工作的,但是到目前为止我测试过的api 19都不起作用,所以可能只有一些最新的api版本支持这个

edit: as @hairraisin mentioned in the comments, try using fgcolor instead of color for the font color, then it should work for lower api levels, but need more testing to be sure

编辑:正如@hairraisin在评论中提到的,尝试使用fgcolor而不是颜色作为字体颜色,那么它应该适用于较低的api级别,但需要更多测试才能确定

#4


13  

With regards to Maneesh's answer, this will work but you need to add and escape the quotes for the color attribute.

关于Maneesh的答案,这将有效,但您需要添加和转义color属性的引号。

myTextView.setText(Html.fromHtml(text + "<font color=\"#FFFFFF\">" + CepVizyon.getPhoneCode() + "</font><br><br>"
            + getText(R.string.currentversion) + CepVizyon.getLicenseText()));

#5


5  

It is good for me!

这对我有好处!

            Spannable spannable = new SpannableString("ABC In-Network DEF");
            String str = spannable.toString();
            iStart = str.indexOf("In-Network");
            iEnd = iStart + 10;/*10 characters = in-network. */

            SpannableString ssText = new SpannableString(spannable);
            ClickableSpan clickableSpan = new ClickableSpan() {
                @Override
                public void onClick(View widget) {
                    //your code at here.
                }

                @Override
                public void updateDrawState(TextPaint ds) {
                    super.updateDrawState(ds);
                    ds.setUnderlineText(true);
                    ds.setColor(getResources().getColor(R.color.green));
                }
            };
            ssText.setSpan(clickableSpan, iStart, iEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            mTextView.setText(ssText);
            mTextView.setMovementMethod(LinkMovementMethod.getInstance());
            mTextView.setHighlightColor(Color.TRANSPARENT);
            mTextView.setEnabled(true);

#6


0  

Here's a colorize function based on andyboot's answer:

这是基于andyboot的答案的colorize函数:

 /**
 * Colorize a specific substring in a string for TextView. Use it like this: <pre>
 * textView.setText(
 *     Strings.colorized("The some words are black some are the default.","black", Color.BLACK),
 *     TextView.BufferType.SPANNABLE
 * );
 * </pre>
 * @param text Text that contains a substring to colorize
 * @param word The substring to colorize
 * @param argb The color
 * @return the Spannable for TextView's consumption
 */
public static Spannable colorized(final String text, final String word, final int argb) {
    final Spannable spannable = new SpannableString(text);
    int substringStart=0;
    int start;
    while((start=text.indexOf(word,substringStart))>=0){
        spannable.setSpan(
                new ForegroundColorSpan(argb),start,start+word.length(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
        );
        substringStart = start+word.length();
    }
    return spannable;
}

#7


-4  

One way is to split myTextView to few separate TextViews, one of which would be only for phone code. Then controlling color of this specific TextView is pretty straight-forward.

一种方法是将myTextView拆分为几个单独的TextView,其中一个仅用于电话代码。然后控制这个特定TextView的颜色非常简单。