Ext.: http://blog.csdn.net/ljz2009y/article/details/23878669
One: The TextView component changes the color of some text:
Java code
- TextView TextView = (TextView) Findviewbyid (R.id.textview);
- Method One:
- Textview.settext (html.fromhtml ("<font color=\" #ff0000 \ "> Red </font> Other Colors"));
- Method Two:
- String Text = "Get the Silver chest!";
- Spannablestringbuilder style=New Spannablestringbuilder (text);
- Style.setspan (new Backgroundcolorspan (color.red),2,5,spannable.span_exclusive_inclusive); //sets the background color of the specified position TextView
- Style.setspan (new Foregroundcolorspan (color.red),0,2,spannable.span_exclusive_inclusive); //sets the color of the text at the specified position
- Textview.settext (style);
Second: the integer and string types in the Android string.xml file are replaced by:
Java code
- String Text = String.Format (Getresources (). getString (R.string.baoxiang), 2,"Silver Treasure Chest");
The corresponding string.xml file parameters:
XML code
- <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:
- TextView TextView = (TextView) Findviewbyid (R.id.testview);
- String Text = String.Format (Getresources (). getString (R.string.baoxiang), 2,"Silver Treasure Chest");
- int index[] = new int[3];
- index[0] = Text.indexof ("2");
- index[1] = Text.indexof ("18");
- index[2] = Text.indexof ("Silver treasure Chest");
- Spannablestringbuilder style=New Spannablestringbuilder (text);
- Style.setspan (new Foregroundcolorspan (color.red), index[0],index[0]+1,spannable.span_exclusive_ INCLUSIVE);
- Style.setspan (new Foregroundcolorspan (color.red), index[1],index[1]+2,spannable.span_exclusive_ INCLUSIVE);
- Style.setspan (new Backgroundcolorspan (color.red), index[2],index[2]+3,spannable.span_exclusive_ INCLUSIVE);
- Textview.settext (style);
Android TextView Change the color of some text and replace the text in String.xml (GO)