Android仿新浪微博、QQ空间等帖子显示(1)
textview通常用来显示普通文本,但是有时候需要对其中某些文本进行样式、事件方面的设置。android系统通过spannablestring类来对指定文本进行相关处理,实际应用中用的比较多的地方比如聊天时显示表情啊,朋友圈或社区中话题的显示、@好友显示和点击等等,关键字显示不同颜色……
1、backgroundcolorspan 背景色
spannablestring spantext = new spannablestring("backgroundcolorspan"); spantext.setspan(new backgroundcolorspan(color.green), 0, spantext.length(), spannable.span_inclusive_exclusive); mtvtext.append("\n"); mtvtext.append(spantext);
2、clickablespan 文本可点击,有点击事件
spannablestring.setspan(new clickablespan() { @override public void onclick(view widget) { spanclicklistener.onspanclick(bean); } @override public void updatedrawstate(textpaint ds) { super.updatedrawstate(ds); //设置画笔属性 ds.setunderlinetext(false);//默认有下划线 } }, matcher.start(), end, spannable.span_exclusive_exclusive); tvtopic.settext(spannablestring); //如果想实现点击,必须要设置这个 tvtopic.setmovementmethod(linkmovementmethod.getinstance());
3、foregroundcolorspan 文本颜色(前景色)
spantext = new spannablestring("这是foregroundcolorspan,看到了吗"); spantext.setspan(new foregroundcolorspan(color.blue), 6, spantext.length(), spannable.span_inclusive_exclusive); mtvtext.append("\n"); mtvtext.append(spantext);
4、maskfilterspan 修饰效果,如模糊(blurmaskfilter)、浮雕(embossmaskfilter)
spantext = new spannablestring("这是maskfilterspan,,看到了吗"); int length = spantext.length(); //模糊(blurmaskfilter) maskfilterspan maskfilterspan = new maskfilterspan(new blurmaskfilter(3, blur.outer)); spantext.setspan(maskfilterspan, 0, length - 10, spannable. span_inclusive_exclusive); //浮雕(embossmaskfilter) maskfilterspan = new maskfilterspan(new embossmaskfilter(new float[]{1,1,3}, 1.5f, 8, 3)); spantext.setspan(maskfilterspan, length - 10, length, spannable.span_inclusive_exclusive); mtvtext.append("\n"); mtvtext.append(spantext);
7、strikethroughspan 删除线(中划线)
spantext = new spannablestring("strikethroughspan"); spantext.setspan(new strikethroughspan(), 0, 7, spannable.span_inclusive_exclusive); mtvtext.append("\n"); mtvtext.append(spantext);
8、suggestionspan
相当于占位符,一般用在edittext输入框中。当双击此文本时,会弹出提示框选择一些建议(推荐的)文字,选中的文本将替换此占位符。在输入法上用的较多。
9、underlinespan 下划线
spantext = new spannablestring("underlinespan"); spantext.setspan(new underlinespan(), 0, spantext.length(), spannable.span_inclusive_exclusive); mtvtext.append("\n"); mtvtext.append(spantext);
10、absolutesizespan 绝对大小(文本字体)
spantext = new spannablestring("absolutesizespan"); spantext.setspan(new absolutesizespan(20, true), 0, spantext.length(), spannable.span_inclusive_exclusive); mtvtext.append("\n"); mtvtext.append(spantext);
11、dynamicdrawablespan 设置图片,基于文本基线或底部对齐。
dynamicdrawablespan drawablespan = new dynamicdrawablespan(dynamicdrawablespan.align_baseline) { @override public drawable getdrawable() { drawable d = getresources().getdrawable(r.drawable.ic_launcher); d.setbounds(0, 0, 50, 50); return d; } }; dynamicdrawablespan drawablespan2 = new dynamicdrawablespan( dynamicdrawablespan.align_bottom) { @override public drawable getdrawable() { drawable d = getresources().getdrawable(r.drawable.ic_launcher); d.setbounds(0, 0, 50, 50); return d; } }; spantext.setspan(drawablespan, 3, 4, spannable.span_inclusive_exclusive); spantext.setspan(drawablespan2, 7, 8, spannable.span_inclusive_exclusive); mtvtext.append("\n"); mtvtext.append(spantext);
12、imagespan 图片
spantext = new spannablestring("imagespan"); drawable d = getresources().getdrawable(r.drawable.ic_launcher); d.setbounds(0, 0, 50, 50); spantext.setspan(new imagespan(d), 3, 4, spannable.span_inclusive_exclusive); mtvtext.append("\n"); mtvtext.append(spantext);
13、relativesizespan 相对大小(文本字体)
spantext = new spannablestring("relativesizespan"); //参数proportion:比例大小 spantext.setspan(new relativesizespan(2.5f), 3, 4, spannable.span_inclusive_exclusive); mtvtext.append("\n"); mtvtext.append(spantext);
14、scalexspan 基于x轴缩放
spantext = new spannablestring("scalexspan"); //参数proportion:比例大小 spantext.setspan(new scalexspan(3.8f), 3, 7, spannable.span_inclusive_exclusive); mtvtext.append("\n"); mtvtext.append(spantext);
15、stylespan 字体样式:粗体、斜体等
spantext = new spannablestring("stylespan"); spantext.setspan(new stylespan(typeface.bold_italic), 3, 7, spannable.span_inclusive_exclusive); mtvtext.append("\n"); mtvtext.append(spantext);
16、subscriptspan 下标(数学公式会用到)
spantext = new spannablestring("subscriptspan"); spantext.setspan(new subscriptspan(), 6, 7, spannable.span_inclusive_exclusive); mtvtext.append("\n"); mtvtext.append(spantext);
17、superscriptspan 上标(数学公式会用到)
spantext = new spannablestring("superscriptspan"); spantext.setspan(new superscriptspan(), 6, 7, spannable.span_inclusive_exclusive); mtvtext.append("\n"); mtvtext.append(spantext);
18、textappearancespan 文本外貌(包括字体、大小、样式和颜色)
spantext = new spannablestring("textappearancespan"); //若需自定义textappearance,可以在系统样式上进行修改 spantext.setspan(new textappearancespan(this, android.r.style.textappearance_medium), 6, 7, spannable.span_inclusive_exclusive); mtvtext.append("\n"); mtvtext.append(spantext);
19、typefacespan 文本字体
spantext = new spannablestring("typefacespan"); spantext.setspan(new typefacespan("monospace"), 3, 10, spannable.span_inclusive_exclusive); mtvtext.append("\n"); mtvtext.append(spantext);
20、urlspan 文本超链接
spantext = new spannablestring("urlspan"); spantext.setspan(new urlspan("http://blog.csdn.net/u011102153"), 10, spantext.length(), spannable.span_inclusive_exclusive); mtvtext.append("\n"); mtvtext.append(spantext); //让urlspan可以点击 mtvtext.setmovementmethod(new linkmovementmethod());
下载:https://github.com/linechen/spannablestringdemo
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。