我是靠谱客的博主 勤恳冷风,这篇文章主要介绍Android 显示后台返回富文本rgb色值适配问题,现在分享给大家,希望可以做个参考。

最近做一个项目经常用到富文本去显示页面,但是富文本修改颜色样式显示不出效果,结果发现后端返回的颜色样式是rgb的格式,Android解析不出这格式,先看下两段一样颜色格式不同展现的效果

复制代码
1
2
3
//直接使用TextView显示富文本 TextView.setText(Html.fromHtml("富文本内容"));
复制代码
1
2
3
4
5
6
<p> <span style="font-size: 14px;">检测到少量色斑,应预防紫外线照射问题,</span> <span style="font-size: 14px; color: #ff0000;">做好肌</span> <span style="font-size: 14px;">肤补水保湿工作,合理的休息,可以让新陈代谢有效地消除脸部色斑。</span> </p>

在这里插入图片描述

复制代码
1
2
3
4
5
6
<p> <span style="font-size: 14px;">检测到少量色斑,应预防紫外线照射问题,</span> <span style="font-size: 14px; color: rgb(230, 0, 0);">做好肌</span> <span style="font-size: 14px;">肤补水保湿工作,合理的休息,可以让新陈代谢有效地消除脸部色斑。</span> </p>

在这里插入图片描述

在不需要后端修改格式的前提下提供一个解决方式, 在别的博客中找到的方法,想链接一下博客地址,但找不到了

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import android.content.Context; import android.content.res.ColorStateList; import android.content.res.Resources; import android.graphics.Color; import android.text.Editable; import android.text.Html; import android.text.Spannable; import android.text.Spanned; import android.text.TextUtils; import android.text.style.AbsoluteSizeSpan; import android.text.style.BackgroundColorSpan; import android.text.style.ForegroundColorSpan; import android.text.style.TextAppearanceSpan; import android.util.Log; import com.djm.smallappliances.utils.DisplayUtils; import org.xml.sax.XMLReader; import java.lang.reflect.Field; import java.util.HashMap; import java.util.Map; /** * created by MMF * on 2020/9/18 * 解決富文本返回颜色格式rgb(230, 0, 0)解析不了问题 */ public class CustomTagHandler implements Html.TagHandler { final HashMap<String, String> attributes = new HashMap<String, String>(); private final String TAG = "CustomTagHandler"; private int startIndex = 0; private int stopIndex = 0; private ColorStateList mOriginColors; private Context mContext; public CustomTagHandler(Context context, ColorStateList originColors) { mContext = context; mOriginColors = originColors; } @Override public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) { processAttributes(xmlReader); //你颜色样式所在的标签 if (tag.equalsIgnoreCase("myspan")) { if (opening) { startSpan(tag, output, xmlReader); } else { endSpan(tag, output, xmlReader); attributes.clear(); } } } private void processAttributes(final XMLReader xmlReader) { try { Field elementField = xmlReader.getClass().getDeclaredField("theNewElement"); elementField.setAccessible(true); Object element = elementField.get(xmlReader); Field attsField = element.getClass().getDeclaredField("theAtts"); attsField.setAccessible(true); Object atts = attsField.get(element); Field dataField = atts.getClass().getDeclaredField("data"); dataField.setAccessible(true); String[] data = (String[]) dataField.get(atts); Field lengthField = atts.getClass().getDeclaredField("length"); lengthField.setAccessible(true); int len = (Integer) lengthField.get(atts); /** * MSH: Look for supported attributes and add to hash map. * This is as tight as things can get :) * The data index is "just" where the keys and values are stored. */ for (int i = 0; i < len; i++) { attributes.put(data[i * 5 + 1], data[i * 5 + 4]); } } catch (Exception e) { } } public void startSpan(String tag, Editable output, XMLReader xmlReader) { startIndex = output.length(); } public void endSpan(String tag, Editable output, XMLReader xmlReader) { stopIndex = output.length(); String color = attributes.get("color"); String bacgroundColor = attributes.get("background-color"); String size = attributes.get("size"); String style = attributes.get("style"); if (!TextUtils.isEmpty(style)) { analysisStyle(startIndex, stopIndex, output, style); } if (!TextUtils.isEmpty(size)) { size = size.split("px")[0]; } if (!TextUtils.isEmpty(bacgroundColor)) { if (bacgroundColor.startsWith("@")) { Resources res = Resources.getSystem(); String name = bacgroundColor.substring(1); int colorRes = res.getIdentifier(name, "color", "android"); if (colorRes != 0) { output.setSpan(new BackgroundColorSpan(colorRes), startIndex, stopIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } } else { if (bacgroundColor.startsWith("rgb")) { bacgroundColor = bacgroundColor.replace("rgb(", ""); bacgroundColor = bacgroundColor.replace(")", ""); String[] rgbs = bacgroundColor.split(", "); bacgroundColor = toHex(Integer.parseInt(rgbs[0]), Integer.parseInt(rgbs[1]), Integer.parseInt(rgbs[2])); } try { output.setSpan(new BackgroundColorSpan(Color.parseColor(bacgroundColor)), startIndex, stopIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } catch (Exception e) { e.printStackTrace(); reductionFontColor(startIndex, stopIndex, output); } } } if (!TextUtils.isEmpty(color)) { if (color.startsWith("@")) { Resources res = Resources.getSystem(); String name = color.substring(1); int colorRes = res.getIdentifier(name, "color", "android"); if (colorRes != 0) { output.setSpan(new ForegroundColorSpan(colorRes), startIndex, stopIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } } else { try { output.setSpan(new ForegroundColorSpan(Color.parseColor(color)), startIndex, stopIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } catch (Exception e) { e.printStackTrace(); reductionFontColor(startIndex, stopIndex, output); } } } if (!TextUtils.isEmpty(size)) { int fontSizePx = 16; if (null != mContext) { fontSizePx = DisplayUtils.dip2sp(mContext, Integer.parseInt(size)); } output.setSpan(new AbsoluteSizeSpan(fontSizePx), startIndex, stopIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } } /** * 解析style属性 * * @param startIndex * @param stopIndex * @param editable * @param style */ private void analysisStyle(int startIndex, int stopIndex, Editable editable, String style) { Log.e(TAG, "style:" + style); String[] attrArray = style.split(";"); Map<String, String> attrMap = new HashMap<>(); if (null != attrArray) { for (String attr : attrArray) { String[] keyValueArray = attr.split(":"); if (null != keyValueArray && keyValueArray.length == 2) { // 记住要去除前后空格 attrMap.put(keyValueArray[0].trim(), keyValueArray[1].trim()); } } } String color = attrMap.get("color"); String bacgroundColor = attrMap.get("background-color"); String fontSize = attrMap.get("font-size"); if (!TextUtils.isEmpty(fontSize)) { fontSize = fontSize.split("px")[0]; } if (!TextUtils.isEmpty(bacgroundColor)) { if (bacgroundColor.startsWith("@")) { Resources res = Resources.getSystem(); String name = bacgroundColor.substring(1); int colorRes = res.getIdentifier(name, "color", "android"); if (colorRes != 0) { editable.setSpan(new BackgroundColorSpan(colorRes), startIndex, stopIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } } else { if (bacgroundColor.startsWith("rgb")) { bacgroundColor = bacgroundColor.replace("rgb(", ""); bacgroundColor = bacgroundColor.replace(")", ""); String[] rgbs = bacgroundColor.split(", "); bacgroundColor = toHex(Integer.parseInt(rgbs[0]), Integer.parseInt(rgbs[1]), Integer.parseInt(rgbs[2])); } try { editable.setSpan(new BackgroundColorSpan(Color.parseColor(bacgroundColor)), startIndex, stopIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } catch (Exception e) { e.printStackTrace(); reductionFontColor(startIndex, stopIndex, editable); } } } if (!TextUtils.isEmpty(color)) { if (color.startsWith("@")) { Resources res = Resources.getSystem(); String name = color.substring(1); int colorRes = res.getIdentifier(name, "color", "android"); if (colorRes != 0) { editable.setSpan(new ForegroundColorSpan(colorRes), startIndex, stopIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } } else { if (color.startsWith("rgb")) { color = color.replace("rgb(", ""); color = color.replace(")", ""); String[] rgbs = color.split(", "); color = toHex(Integer.parseInt(rgbs[0]), Integer.parseInt(rgbs[1]), Integer.parseInt(rgbs[2])); } try { editable.setSpan(new ForegroundColorSpan(Color.parseColor(color)), startIndex, stopIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } catch (Exception e) { e.printStackTrace(); reductionFontColor(startIndex, stopIndex, editable); } } } if (!TextUtils.isEmpty(fontSize)) { int fontSizePx = 14; if (null != mContext) { fontSizePx = DisplayUtils.dip2sp(mContext, Integer.parseInt(fontSize)); } editable.setSpan(new AbsoluteSizeSpan(fontSizePx), startIndex, stopIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } } /** * 还原为原来的颜色 * * @param startIndex * @param stopIndex * @param editable */ private void reductionFontColor(int startIndex, int stopIndex, Editable editable) { if (null != mOriginColors) { editable.setSpan(new TextAppearanceSpan(null, 0, 0, mOriginColors, null), startIndex, stopIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } else { editable.setSpan(new ForegroundColorSpan(0xff2b2b2b), startIndex, stopIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } } public static String toHex(int r, int g, int b) { return "#" + toBrowserHexValue(r) + toBrowserHexValue(g) + toBrowserHexValue(b); } private static String toBrowserHexValue(int number) { StringBuilder builder = new StringBuilder( Integer.toHexString(number & 0xff)); while (builder.length() < 2) { builder.append("0"); } return builder.toString().toUpperCase(); } }

使用方式:

复制代码
1
2
3
String htmlStr = "富文本内容".replace("span", "myspan"); TextView.setText(Html.fromHtml(htmlStr, null, new CustomTagHandler(mContext, holder.tvCause.getTextColors())));

Android8.0还是9.0会过滤span标签导致handleTag方法监听不到span标签,所以修改为"myspan"(具体Android版本查清再来更新),“myspan”这个标签可以按自己想的去定义,定义后在CustomTagHandler中的handleTag监听中的判断要改成自己定义的标签

最后

以上就是勤恳冷风最近收集整理的关于Android 显示后台返回富文本rgb色值适配问题的全部内容,更多相关Android内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(54)

评论列表共有 0 条评论

立即
投稿
返回
顶部