The International UI is adjusted in the project, because the Chinese and English are different, so the knowledge here is used:
Java code
- Textview = (textview) findviewbyid (R. Id. textview );
- // Method 1:
- Textview. settext (html. fromhtml ("<font color = \" # ff0000 \ "> Red </font> other colors "));
- // Method 2:
- String text = "get silver treasure box! ";
- Spannablestringbuilder style = new spannablestringbuilder (text );
- Style. setspan (New backgroundcolorspan (color. Red), spannable. span_exclusive_wide SIVE); // you can specify the background color of the textview.
- Style. setspan (New foregroundcolorspan (color. Red), spannable. span_exclusive_wide SIVE); // you can specify the color of the text at the specified position.
- Textview. settext (style );
Ii. Replacement of integer and string types in the android string. xml file:
Java code
- String text = string. Format (getresources (). getstring (R. String. baoxiang), "silver chest ");
Corresponding string. xml file parameters:
XML Code
- <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
- Textview = (textview) findviewbyid (R. Id. testview );
- String text = string. Format (getresources (). getstring (R. String. baoxiang), "silver chest ");
- Int index [] = new int [3];
- Index [0] = text. indexof ("2 ");
- Index [1] = text. indexof ("18 ");
- Index [2] = text. indexof ("silver 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 );