時候一個文本框爲了強調內容須要顯示不一樣顏色,用如下代碼能夠輕鬆實現ui
方法一:(適用於顏色變化多的狀況)spa
//爲文本框設置多種顏色 textView=(TextView)findViewById(R.id.text_show); SpannableStringBuilder style = new SpannableStringBuilder("備註:簽收人(張三)"); style.setSpan(new ForegroundColorSpan(Color.BLUE), 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); style.setSpan(new ForegroundColorSpan(Color.RED), 7, 9, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); textView.setText(style);
方法二:(高校,快捷)code
首先在字符串中就設置好顏色屬性orm
String str1=String.format("價格 :<font color=\"#d40000\">%s", String.format("¥%1$.2f元", item.getPrice())); String str2=String.format("狀態 :<font color=\"#666666\">%s", "已售");
而後用Html.fromHtml()方法blog
mStaringPriceTV.setText(Html.fromHtml(str1)); mCountdownTimeTV.setText(Html.fromHtml(str2));