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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
最新评论