Android一個TextView設置多種顏色的2種高效方法
阿新 • • 發佈:2017-06-19
track 能夠 post star strong 價格 show img clas
有時候一個文本框為了強調內容須要顯示不同顏色,用下面代碼能夠輕松實現
方法一:(適用於顏色變化多的情況)
//為文本框設置多種顏色 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", "已售");
mStaringPriceTV.setText(Html.fromHtml(str1)); mCountdownTimeTV.setText(Html.fromHtml(str2));
像這樣的情況用一個文本框來實現會非常省事
Android一個TextView設置多種顏色的2種高效方法