How to set color of UnderlineSpan?

与世无争的帅哥 提交于 2019-12-04 03:44:29
Lisa Wray

Unfortunately, you can't do this solely with a Span -- there are no public methods to do what you ask. There is a method to set the underline color in TextPaint independent of the text color (you can see it in the source code for SuggestionSpan) but it's hidden.

If you can get access to the EditText, you can handle the Span and draw the underline yourself, but otherwise you are out of luck.

here is the function which you can use to set color of line below text

public void addUnderLineLink(TextView mTextView) {
    if (mTextView != null) {
        SpannableString content = new SpannableString(mTextView.getText().toString());
        UnderlineSpan us=new UnderlineSpan();
        TextPaint tp=new TextPaint();
        tp.setColor(ContextCompat.getColor(getContext(),R.color.white));
        us.updateDrawState(tp);
        content.setSpan(us, 0, mTextView.getText().toString().length(), 0);
        mTextView.setText(content);
    }
}

This is how i made it work

Spanned part_1 = Html.fromHtml(getString(R.string.part1));
Spanned part_2 = Html.fromHtml("<u><font color='#2eb6f0'>Text in blue</font></u>");
Spanned part_3 = Html.fromHtml(getString(R.string.part3));
Spanned output = (Spanned) TextUtils.concat(part_1, part_2, part_3);

textview.setText(output, TextView.BufferType.SPANNABLE);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!