先看效果
使用方法:
代码很简单相信大家都看得懂
接下来上工具类代码
public class SpannableStringUtil {
private SpannableStringBuilder builder=new SpannableStringBuilder();
private ListinfoList=new ArrayList<>();
public static SpannableStringUtil create() {
return new SpannableStringUtil();
}
public SpannableStringUtil setText(String text) {
builder.append(text);
infoList.add(newStringInfo());
StringInfo lastInfo = getLastInfo();
lastInfo.startIndex=builder.length()- text.length();
lastInfo.endIndex=builder.length();
return this;
}
public SpannableStringUtil setTextColor(intcolor) {
ForegroundColorSpan span =new ForegroundColorSpan(color);
StringInfo stringInfo = getLastInfo();
stringInfo.styleList.add(span);
return this;
}
public SpannableStringUtil setTextColor(Context context, intcolorId) {
ForegroundColorSpan span =new ForegroundColorSpan(ContextCompat.getColor(context,colorId));
StringInfo stringInfo = getLastInfo();
stringInfo.styleList.add(span);
return this;
}
public SpannableStringUtil setBgColor(intcolor) {
BackgroundColorSpan span =new BackgroundColorSpan(color);
StringInfo stringInfo = getLastInfo();
stringInfo.styleList.add(span);
return this;
}
publicSpannableStringUtilsetBgColor(Context context, intcolorId) {
BackgroundColorSpan span =newBackgroundColorSpan(ContextCompat.getColor(context,colorId));
StringInfo stringInfo = getLastInfo();
stringInfo.styleList.add(span);
return this;
}
publicSpannableStringUtilsetRelativeSize(floatsize) {
RelativeSizeSpan span =newRelativeSizeSpan(size);
StringInfo stringInfo = getLastInfo();
stringInfo.styleList.add(span);
return this;
}
publicSpannableStringUtilsetImg(Drawable drawable) {
ImageSpan span =newImageSpan(drawable);
builder.append("0");
infoList.add(newStringInfo());
StringInfo lastInfo = getLastInfo();
lastInfo.startIndex=builder.length()-1;
lastInfo.endIndex=builder.length();
lastInfo.styleList.add(span);
return this;
}
publicSpannableStringUtilsetClick(TextView textView, finalOnClickListener listener) {
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setHighlightColor(Color.parseColor("#36969696"));
finalStringInfo lastInfo = getLastInfo();
ClickableSpan span =newClickableSpan() {
@Override
public voidupdateDrawState(TextPaint ds) {}
@Override
public voidonClick(View widget) {
listener.onClick(
builder.subSequence(lastInfo.startIndex,lastInfo.endIndex).toString());
}
};
lastInfo.styleList.add(span);
return this;
}
publicSpannableStringUtilsetStyle(booleanbold, booleanintalic) {
StringInfo stringInfo = getLastInfo();
if(bold && intalic) {
StyleSpan span =newStyleSpan(Typeface.BOLD_ITALIC);
stringInfo.styleList.add(span);
}else if(bold){
StyleSpan span =newStyleSpan(Typeface.BOLD);
stringInfo.styleList.add(span);
}else if(intalic) {
StyleSpan span =newStyleSpan(Typeface.ITALIC);
stringInfo.styleList.add(span);
}
return this;
}
publicSpannableStringBuilderbuild() {
for(inti =0;i
StringInfo stringInfo =infoList.get(i);
for(inti1 =0;i1 < stringInfo.styleList.size();i1++) {
CharacterStyle style = stringInfo.styleList.get(i1);
if(style !=null)
builder.setSpan(style,stringInfo.startIndex,stringInfo.endIndex
,Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
}
returnbuilder;
}
privateStringInfogetLastInfo() {
returninfoList.get(infoList.size() -1);
}
public interfaceOnClickListener{
voidonClick(String clickStr);
}
private classStringInfo {
intstartIndex;
intendIndex;
ListstyleList=newArrayList<>();
}
}