Android TextView Change the color of some text and replace the text in String.xml

Source: Internet
Author: User

Ext.: http://blog.csdn.net/ljz2009y/article/details/23878669

One: The TextView component changes the color of some text:

Java code
  1. TextView TextView = (TextView) Findviewbyid (R.id.textview);
  2. Method One:
  3. Textview.settext (html.fromhtml ("<font color=\" #ff0000 \ "> Red </font> Other Colors"));
  4. Method Two:
  5. String Text = "Get the Silver chest!";
  6. Spannablestringbuilder style=New Spannablestringbuilder (text);
  7. Style.setspan (new Backgroundcolorspan (color.red),2,5,spannable.span_exclusive_inclusive); //sets the background color of the specified position TextView
  8. Style.setspan (new Foregroundcolorspan (color.red),0,2,spannable.span_exclusive_inclusive); //sets the color of the text at the specified position
  9. Textview.settext (style);

Second: the integer and string types in the Android string.xml file are replaced by:

Java code
    1. String Text = String.Format (Getresources (). getString (R.string.baoxiang), 2,"Silver Treasure Chest");

The corresponding string.xml file parameters:

XML code
    1. <string name="Baoxiang"> You played the%1$d today, and the%2$D bureau can get%3$s! </string>

%1$d expression means the entire Name= "Baoxiang" string, the first integral type

In project developers, it is often necessary to combine the two. You can avoid many textview stitching, as shown below:

  1. TextView TextView = (TextView) Findviewbyid (R.id.testview);
  2. String Text = String.Format (Getresources (). getString (R.string.baoxiang), 2,"Silver Treasure 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 treasure 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);

Android TextView Change the color of some text and replace the text in String.xml (GO)

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.