Set color of TextView span in Android

前端 未结 15 1518
眼角桃花
眼角桃花 2020-11-22 05:54

Is it possible to set the color of just span of text in a TextView?

I would like to do something similar to the Twitter app, in which a part of the text is blue. See

15条回答
  •  遥遥无期
    2020-11-22 06:25

    Set your TextView´s text spannable and define a ForegroundColorSpan for your text.

    TextView textView = (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);    
    textView.setText(wordtoSpan);
    

提交回复
热议问题