Android TextView 實現文字大小不同和文字顏色不同

來源:互聯網
上載者:User

標籤:android   style   class   blog   code   java   

如下:


關鍵代碼如下:

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(""); // 過濾html標籤                SpannableString span = new SpannableString(resultStr);                for (URLSpan url : urls) {                    int startIndex = ss.getSpanStart(url);                    int endIndex = ss.getSpanEnd(url);                    // 原字兩倍大小                    span.setSpan(new AbsoluteSizeSpan(Global.dpToPx(24)), startIndex, endIndex,                            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);                    // 設定樣式2                    span.setSpan(new ForegroundColorSpan(Color.WHITE),                            startIndex, endIndex,                            Spannable.SPAN_EXCLUSIVE_INCLUSIVE);                    // 設定樣式3                    span.setSpan(new StyleSpan(Typeface.BOLD),                            startIndex, endIndex,                            Spannable.SPAN_EXCLUSIVE_INCLUSIVE);                }                mBottomLL2_Detail.setText(span);

說明:

1.主要原理為使用SpannableString來做多樣式的String給TextView顯示

2.用到<a href>標籤的原因是為了做到國際化,因為span是按照index來多樣化的,所以需要用href來標識多樣化的index

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.