简体   繁体   中英

Different color of texts in the same textview

I am using following code

 Spannable wordtoSpan = new SpannableString(getString(R.string.signup_text));
    wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLACK), 0, 18, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    signupText.setText(wordtoSpan);

However my textView is showing the same color as set in its xml ie skin color.

I want to show substring from 0 - 18 as BLACK and rest color as skin color. What is wrong here?

Try like this :

Resources res = getResources();

Spannable wordtoSpan = new SpannableString(res.getString(R.string.signup_text));

I dunno whether you are trying to use two colors in the same string if so,You could try some thing like this

In your strings.xml

  <string name="hello"><![CDATA[ TEXT IN BLACK <b><font color=#ff0000>TEXT IN RED</b>]]></string>

in your activity

textview.setText(Html.fromHtml(getString(R.string.hello)));

Then TEXT IN BLACK will be seen in normal color and TEXT IN RED will be shown in red color

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM