在平时的开发中经常遇到文本中带有数字或者字母之类改变文本颜色添加下划线,或在文本左侧、右侧添加图片等需求,这里自己写了个非常实用的类,定义颜色图片等(支持多规则)
首先看效果图:
自定义DiyStyleTextView继承自AppCompatTextView
文本是否显示下划线
public DiyStyleTextViewsetUnderlineText(boolean underlineText) {
this.underlineText = underlineText;
return this;
}
添加自定义颜色规则,满足该正则表达式的文本自定义颜色,colorRegex正则表达式,color自定义的颜色
public DiyStyleTextViewaddColorRegex(String colorRegex, int color) {
setMovementMethod(LinkMovementMethod.getInstance());
colorRegexList.add(colorRegex);
colorList.add(color);
return this;
}
添加自定义图片规则
public DiyStyleTextViewaddImageRegex(String imageRegex, Bitmap bitmap) {
setMovementMethod(LinkMovementMethod.getInstance());
imageRegexList.add(imageRegex);
bitmapList.add(bitmap);
return this;
}
最后贴上代码:
public class DiyStyleTextViewextends AppCompatTextView {
private boolean underlineText =false;
private ListcolorRegexList =new ArrayList<>();
private ListcolorList =new ArrayList<>();
private ListimageRegexList =new ArrayList<>();
private ListbitmapList =new ArrayList<>();
public DiyStyleTextView(Context context) {
super(context);
}
public DiyStyleTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
//是否显示下划线-全局
public DiyStyleTextViewsetUnderlineText(boolean underlineText) {
this.underlineText = underlineText;
return this;
}
//添加自定义颜色规则
public DiyStyleTextViewaddColorRegex(String colorRegex, int color) {
setMovementMethod(LinkMovementMethod.getInstance());
colorRegexList.add(colorRegex);
colorList.add(color);
return this;
}
//添加自定义图片规则
public DiyStyleTextViewaddImageRegex(String imageRegex, Bitmap bitmap) {
setMovementMethod(LinkMovementMethod.getInstance());
imageRegexList.add(imageRegex);
bitmapList.add(bitmap);
return this;
}
//覆盖父类,setText()生效
@Override
public void setText(CharSequence text, BufferType type) {
super.setText(setTextStyle(text, false), type);
}
//覆盖父类,append()生效
@Override
public void append(CharSequence text, int start, int end) {
super.append(setTextStyle(text, false), start, end);
}
public void setDiyTextColor(CharSequence text, String regularExpression, int color, DiyTextClick mDiyTextClick) {
addColorRegex(regularExpression, color).setDiyTextClickListenner(mDiyTextClick).setTextStyle(text, true);
}
public void setDiyTextColor(CharSequence text, String regularExpression, int color) {
setDiyTextColor(text, regularExpression, color, null);
}
public void setDiyTextImage(CharSequence text, String regularExpression, Bitmap bitmap, DiyTextClick mDiyTextClick) {
addImageRegex(regularExpression, bitmap).setDiyTextClickListenner(mDiyTextClick).setTextStyle(text, true);
}
public void setDiyTextImage(CharSequence text, String regularExpression, Bitmap bitmap) {
setDiyTextImage(text, regularExpression, bitmap, null);
}
private ListindexArr =new ArrayList<>();
private ListstrArr =new ArrayList<>();
public CharSequencesetTextStyle(CharSequence text, boolean flag) {
if (TextUtils.isEmpty(text)) {
if (flag)super.setText(text);
return text;
}
SpannableStringBuilder styledText =new SpannableStringBuilder(text);
if (colorRegexList !=null)
for (int j =0; j
String colorRegex =colorRegexList.get(j);
int color =colorList.get(j);
indexArr.clear();
strArr.clear();
//正则获取符合的字符串
Pattern p = Pattern.compile(colorRegex);
Matcher m = p.matcher(text);
while (m.find()) {
strArr.add(m.group());
indexArr.add(m.start());
}
for (int i =0; i
int index =indexArr.get(i);
String clickText =strArr.get(i);
//替换文本 自定义风格
styledText.setSpan(
diyTextClickListenner ==null ?
new TextViewCharacterStyle(color) :
new TextViewClickSpan(clickText, color),
index,
index + clickText.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
if (imageRegexList !=null)
for (int j =0; j
String imageRegex =imageRegexList.get(j);
Bitmap bitmap =bitmapList.get(j);
indexArr.clear();
strArr.clear();
Pattern p = Pattern.compile(imageRegex);
Matcher m = p.matcher(text);
while (m.find()) {
strArr.add(m.group());
indexArr.add(m.start());
}
for (int i =0; i
int index =indexArr.get(i);
String clickText =strArr.get(i);
styledText.setSpan(
new ImageSpan(bitmap),
index,
index + clickText.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
styledText.setSpan(
diyTextClickListenner ==null ?
null :
new TextViewClickSpan(clickText, 0),
index,
index + clickText.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
if (flag)super.setText(styledText);
return styledText;
}
//设置颜色 点击监听的Span
private class TextViewClickSpanextends ClickableSpan {
private StringclickText;
private int color;
TextViewClickSpan(String clickText) {
this.clickText = clickText;
}
TextViewClickSpan(String clickText, int color) {
this.clickText = clickText;
this.color = color;
}
@Override
public void updateDrawState(TextPaint ds) {
if (color !=0) ds.setColor(color);
ds.setUnderlineText(underlineText); //下划线
}
@Override
public void onClick(View widget) {//点击事件
if (diyTextClickListenner !=null)
diyTextClickListenner.diyTextClick(clickText);
}
}
//只有设置颜色Span
private class TextViewCharacterStyleextends CharacterStyle {
private int color;
TextViewCharacterStyle(int color) {
this.color = color;
}
@Override
public void updateDrawState(TextPaint ds) {
if (color !=0) ds.setColor(color);
ds.setUnderlineText(underlineText); //下划线
}
}
private DiyTextClickdiyTextClickListenner;
public interface DiyTextClick {
void diyTextClick(String s);
}
//点击监听-全局
public DiyStyleTextViewsetDiyTextClickListenner(DiyTextClick mDiyTextClick) {
this.diyTextClickListenner = mDiyTextClick;
setClickable(true);
return this;
}
}
使用方法:
diytextview.addColorRegex("[\\d]+", 0xff2CA298)
.addColorRegex("[a-zA-Z]+", 0xffff6666)
.addImageRegex("\\[d_aini\\]", BitmapFactory.decodeResource(getResources(), R.drawable.d_aini))
.addImageRegex("\\[d_doge\\]", BitmapFactory.decodeResource(getResources(), R.drawable.d_doge));
diytextview.setText("[d_doge]热爱工作Work2018[d_aini]");
diytextview.append("\n[d_doge]热爱生活Life2018[d_aini]");
diytextview.append("\n[d_doge]热爱学习Learn1314[d_aini]");
diytextview.append("\n[d_doge]热爱学习Learn1314[d_aini]");
diytextview.append("\n[d_doge]热爱学习Learn1314[d_aini]");
diytextview.append("\n[d_doge]热爱学习Learn1314[d_aini]");
diytextview.append("\n[d_doge]热爱学习Learn1314[d_aini]");