時候一個文本框為了強調內容需要顯示不同顏色,用以下代碼可以輕松實現
方法一:(適用於顏色變化多的情況)
//為文本框設置多種顏色 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);

方法二:(高校,快捷)
首先在字符串中就設置好顏色屬性
String str1=String.format("價格 :<font color=\"#d40000\">%s", String.format("¥%1$.2f元", item.getPrice()));
String str2=String.format("狀態 :<font color=\"#666666\">%s", "已售");
然后用Html.fromHtml()方法
mStaringPriceTV.setText(Html.fromHtml(str1));
mCountdownTimeTV.setText(Html.fromHtml(str2));
