如何在文本视图中更改字符串的某些部分的颜色,该值来自json。我同时使用Spannable类和通过html,但未更改。
if(jobject.has("posts")){
JSONArray ar= jobject.getJSONArray("posts");
String value=""+ar.length();
Profile.this.post.setText(value);
for(int i=0;i<ar.length();i++){
JSONObject a= ar.getJSONObject(i);
Pojo_profile po= new Pojo_profile();
// po.setUsername();
String blue = object.getString("user_login");
po.setData(Html.fromHtml("<font color='#FF0000'>"+blue+"</font>")+" "+a.getString("text_post"));
profile.add(po);
}
使用跨度。
例子:
final SpannableStringBuilder sb = new SpannableStringBuilder("your text here");
// Span to set text color to some RGB value
final ForegroundColorSpan fcs = new ForegroundColorSpan(Color.rgb(158, 158, 158));
// Span to make text bold
final StyleSpan bss = new StyleSpan(android.graphics.Typeface.BOLD);
// Set the text color for first 4 characters
sb.setSpan(fcs, 0, 4, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
// make them also bold
sb.setSpan(bss, 0, 4, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
yourTextView.setText(sb);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句