As follows:
The key code is as follows:
[Java] view Plaincopy
- StringBuffer sb = new StringBuffer ();
- if (Day > 0) {
- Sb.append ("<a href=\" 1\ ">");
- Sb.append ("+day");
- Sb.append ("</a>");
- Sb.append ("+global.getstring (r.string.unit_day2)");
- }
- Sb.append ("<a href=\" 2\ ">");
- if (Hour > 9) {
- Sb.append ("+hour");
- } Else {
- Sb.append ("0" +hour);
- }
- Sb.append ("</a>");
- Sb.append ("+global.getstring (R.STRING.UNIT_HOUR2)");
- Sb.append ("<a href=\" 3\ ">");
- if (Minute > 9) {
- Sb.append ("+minute");
- } Else {
- Sb.append ("0" +minute);
- }
- Sb.append ("</a>");
- Sb.append ("+global.getstring (R.string.unit_minute2)");
- Sb.append ("<a href=\" 4\ ">");
- if (Second > 9) {
- Sb.append ("" + second);
- } Else {
- Sb.append ("0" + second);
- }
- Sb.append ("</a>");
- Sb.append ("+global.getstring (R.string.unit_second)");
- int end = Sb.length ();
- spannablestring ss = New Spannablestring (html.fromhtml (sb.tostring ()));
- urlspan[] urls = Ss.getspans (0, End, Urlspan. Class);
- String resultstr = sb.tostring ();
- Pattern p_html = Pattern.compile (regex_html, pattern.case_insensitive);
- Matcher m_html = P_html.matcher (RESULTSTR);
- ResultStr = M_html.replaceall (""); //Filter HTML tags
- Spannablestring span = new spannablestring (RESULTSTR);
- For (Urlspan url:urls) {
- int startIndex = Ss.getspanstart (URL);
- int endIndex = ss.getspanend (URL);
- //twice times the original word size
- Span.setspan (new Absolutesizespan (global.dptopx), StartIndex, EndIndex,
- spannable.span_exclusive_exclusive);
- //Set style 2
- Span.setspan (new Foregroundcolorspan (color.white),
- StartIndex, EndIndex,
- spannable.span_exclusive_inclusive);
- //Set style 3
- Span.setspan (new Stylespan (typeface.bold),
- StartIndex, EndIndex,
- spannable.span_exclusive_inclusive);
- }
- Mbottomll2_detail.settext (span);
Description
1. The main principle is to use spannablestring to long style string to TextView display
2. The reason for using the <a href> label is to be internationalized, because span is diversified according to index, so it is necessary to use HREF to identify a variety of index
Android TextView Achieve different text sizes and text colors