SpannableString同时设置了ClickableSpan和ForegroundColorSpan后,发现ForegroundColorSpan不生效。
原因:ClickableSpan将ForegroundColorSpan的颜色覆盖了解决方式:将ForegroundColorSpan替换为UnderlineSpan,并重写updateDrawState方法
SpannableString protocol = new SpannableString(getResources().getString(R.string.protocol));
protocol.setSpan(new ClickableSpan() {
@Override
public void onClick(View widget) {
Intent intent = new Intent();
intent.setClass(context, WebViewActivity.class);
startActivity(intent);
}
}, 13, 23, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
protocol.setSpan(new UnderlineSpan() {
@Override
public void updateDrawState(TextPaint ds) {
ds.setColor(Color.BLUE));//设置颜色
ds.setUnderlineText(false);//去掉下划线
}
}, 13, 23, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
privacyProtocol.setText(protocol);
privacyProtocol.setMovementMethod(LinkMovementMethod.getInstance());