UnderlineSpan

下划线样式。

示例1:

UnderlineSpan underlineSpan = new UnderlineSpan();

String text = "打开百度";
SpannableString spannableString = new SpannableString(text);
spannableString.setSpan(underlineSpan, 2, text.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);

TextView textView = (TextView) findViewById(R.id.text_view);
textView.setText(spannableString);

示例2:

Parcel parcel = Parcel.obtain();
parcel.writeString("http://www.baidu.com");
parcel.setDataPosition(0);
UnderlineSpan underlineSpan = new UnderlineSpan(parcel);

String text = "打开百度";
SpannableString spannableString = new SpannableString(text);
spannableString.setSpan(underlineSpan, 2, text.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);

TextView textView = (TextView) findViewById(R.id.text_view);
textView.setText(spannableString);

示例3:

重写updateDrawState(TextPaint textPaint)方法,修改下划线的样式、文本颜色:

UnderlineSpan underlineSpan = new UnderlineSpan() {
    @Override
    public void updateDrawState(TextPaint textPaint) {
        textPaint.setColor(Color.argb(255, 54, 92, 124));
        textPaint.setUnderlineText(true);
    }
};

String text = "打开百度";
SpannableString spannableString = new SpannableString(text);
spannableString.setSpan(underlineSpan, 2, text.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);

TextView textView = (TextView) findViewById(R.id.text_view);
textView.setText(spannableString);