你们能告诉我如何在textview中编写java代码,我希望它像我编写java代码一样着色。 我发现了一个由2种类型的文本组成的例子,我认为它们都是TextView,但是seconde包含Highlight文本,左边是绿色条,你们可以告诉我们如何做到这一点。 (抱歉,我无法上传图片)
答案 0 :(得分:1)
试试这个,
public static void setText(TextView textView, String text) {
Spannable spannable = new SpannableString(text);
// For Foreground Highlight
spannable.setSpan(new ForegroundColorSpan(fromColor(ColorCode)), highlight.start, highlight.end, 0);
// For Background Highlight
spannable.setSpan(new BackgroundColorSpan(fromColor(ColorCode)), highlight.start, highlight.end, 0);
textView.setText(spannable);
}
答案 1 :(得分:0)
试试这个,
TextView TV = (TextView)findViewById(R.id.mytextview01);
Spannable wordtoSpan = new SpannableString("I know just how to whisper, And I know just how to cry,I know just where to find the answers");
wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 15, 30, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
TV.setText(wordtoSpan);
原始答案here