Set color of TextView span in Android

前端 未结 15 1493
眼角桃花
眼角桃花 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:27

    If you want more control, you might want to check the TextPaint class. Here is how to use it:

    final ClickableSpan clickableSpan = new ClickableSpan() {
        @Override
        public void onClick(final View textView) {
            //Your onClick code here
        }
    
        @Override
        public void updateDrawState(final TextPaint textPaint) {
            textPaint.setColor(yourContext.getResources().getColor(R.color.orange));
            textPaint.setUnderlineText(true);
        }
    };
    

提交回复
热议问题