2012-08-08 74 views
0

我想从textview的某个位置突出显示Textview的特定颜色。例如,在textview字符串中,我想突出显示从0到15位置的白色和从16到红色的String.Is结束有可能吗?任何人都可以给我一个例子吗?着色文本视图

回答

2

通过使用Spannable Text你可以做到这一点

Spannable WordtoSpan = new SpannableString("I know just how to whisper");   
WordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 0, 15, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
tv.setText(WordtoSpan); 
+0

我能够与您的代码来改变颜色,但是当我在getview使用()的一个表项刷新的ListView的方法原来的color.how得到这个解决? – Debtaru 2012-08-08 08:43:41

0

TextView有一个属性textColor。一旦设置它会着色整个TextView. 我不认为有什么内置的Android内置来实现你所需要的,但一个想法是从你需要的位置截取你的长TextView,并将其分别放入几个小的TextViews中,自己的颜色。

0
Get count of your string 

    int length = YourString.length(); 
then check with if condtion and set like this. 

    TextView tt; 
    int color = Integer.parseInt("bdbdbd", 16)+0xFF000000); 
    tt.setTextColor(color); 
0

也许你可以使用的WebView并把里面的HTML代码...

String html = "<div><span style="background-color:red">SOME TEXT</span><span style="background-color:blue">SOME TEXT</span></div>" 
wv.loadData(html, "text/html", Encoding.UTF_8.toString()); 
0

我不认为这是有直接方法在文本视图中设置不同的颜色。

你可以尝试设置html格式的文本到你的文本视图来实现你的目标。例如, ;

yourTextView.setText(Html.fromHtml(yourText)); 
在上面的代码

yourText是HTML格式的字符串,你可以创建HTML文本按照你的应用逻辑。

0

你可以试试这个与ForegroundColorSpanTextView例如 -

TextView tView = (TextView)findViewById(R.id.text); 
tView.setText("Italic, highlighted, bold.", TextView.BufferType.SPANNABLE); 
Spannable WordtoSpan = (Spannable) tView.getText(); 
WordtoSpan.setSpan(new ForegroundColorSpan(0xFFFFFF00), 0, 15, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
tView.setText(WordtoSpan);