Android textview changes the color of some text and replaces the text in string. xml.

Source: Internet
Author: User

The International UI is adjusted in the project, because the Chinese and English are different, so the knowledge here is used:

Java code
  1. Textview = (textview) findviewbyid (R. Id. textview );
  2. // Method 1:
  3. Textview. settext (html. fromhtml ("<font color = \" # ff0000 \ "> Red </font> other colors "));
  4. // Method 2:
  5. String text = "get silver treasure box! ";
  6. Spannablestringbuilder style = new spannablestringbuilder (text );
  7. Style. setspan (New backgroundcolorspan (color. Red), spannable. span_exclusive_wide SIVE); // you can specify the background color of the textview.
  8. Style. setspan (New foregroundcolorspan (color. Red), spannable. span_exclusive_wide SIVE); // you can specify the color of the text at the specified position.
  9. Textview. settext (style );

 

 

 

 

Ii. Replacement of integer and string types in the android string. xml file:

 

Java code
  1. String text = string. Format (getresources (). getstring (R. String. baoxiang), "silver chest ");

 

Corresponding string. xml file parameters:

 

XML Code
  1. <String name = "baoxiang"> you played the % 1 $ D game today. % 2 $ D game! % 3 $ s! </String>

% 1 $ D indicates the first integer in the entire name = "baoxiang" string.

 

 

Project developers often need to combine the above two. You can avoid splicing many textviews, as shown below:

 

Java code

  1. Textview = (textview) findviewbyid (R. Id. testview );
  2. String text = string. Format (getresources (). getstring (R. String. baoxiang), "silver chest ");
  3. Int index [] = new int [3];
  4. Index [0] = text. indexof ("2 ");
  5. Index [1] = text. indexof ("18 ");
  6. Index [2] = text. indexof ("silver chest ");
  7. Spannablestringbuilder style = new spannablestringbuilder (text );
  8. Style. setspan (New foregroundcolorspan (color. Red), index [0], index [0] + 1, spannable. span_exclusive_inclusive );
  9. Style. setspan (New foregroundcolorspan (color. Red), index [1], index [1] + 2, spannable. span_exclusive_inclusive );
  10. Style. setspan (New backgroundcolorspan (color. Red), index [2], index [2] + 3, spannable. span_exclusive_inclusive );
  11. Textview. settext (style );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.