SpannableString 配置模板的显示样式,在这里记录一下。
1、读取assets文件夹下配置文件的信息,json格式的数据
[{"code": "[sms_order_type]","key": "Order Type"},{"code": "[sms_phone_number]","key":"Phone Number"}]
2、根据正则表达式替换显示
正则替换采用ImageSpan 替换显示
private void dealExpression(Context context, SpannableString spannableString, Pattern patten, int start,HashMap recordUsedMap) {
Matcher matcher = patten.matcher(spannableString);
while (matcher.find()) {
String key = matcher.group();
if (matcher.start() < start) {
continue;
}
String showStr =editShowMap.get(key);
if (TextUtils.isEmpty(showStr)) {
continue;
}
recordUsedMap.put(key,showStr);
TextView styleTv =new TextView(context);
styleTv.setText(showStr);
styleTv.setTextColor(ContextCompat.getColor(context,R.color.app_color_white));
styleTv.setTextSize(SizeUtils.dp2px(6f));
styleTv.setBackgroundResource(R.drawable.shape_sms_button_bg_sel);
styleTv.setMinWidth(SizeUtils.dp2px(100f));
styleTv.setGravity(Gravity.CENTER);
styleTv.setPadding(20,5,20,5);
styleTv.setDrawingCacheEnabled(true);
styleTv.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
styleTv.layout(0, 0, styleTv.getMeasuredWidth(), styleTv.getMeasuredHeight());
Bitmap bitmap = Bitmap.createBitmap(styleTv.getDrawingCache());
int end = matcher.start() + key.length();
ImageSpan imageSpan =new ImageSpan(context, bitmap);
spannableString.setSpan(imageSpan, matcher.start(), end, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
styleTv.destroyDrawingCache();
if (end < spannableString.length()) {
dealExpression(context, spannableString, patten, end,recordUsedMap);
}
break;
}
}
在读取配置模板时,再根据正则表达式进行标签替换
网友评论