Android中实现不一样文字颜色和图文混排的Span总结

1、怎么在TextView中设置首行缩进两个字符java

在string资源文件中,在文字的前面加入”\u3000\u3000”便可实现首行缩进android

在Java代码中,使用setText("\u3000\u3000"+xxxxx);markdown

2、TextView中的图文混排和不一样颜色、大小字体的显示app

方法一:设置不一样颜色、大小、图文混排的效果经过SpannableString,而且设置各类Span实现的。ide

SpannableString的setSpan方法须要几个参数:工具

public void setSpan (Object what, int start, int end, int flags)复制代码

what传入各类Span类型的实例,start和end标记要替代的文字内容的范围,flags是用来标识在 Span 范围内的文本先后输入新的字符时是否把它们也应用这个效果,能够传入Spanned.SPAN_EXCLUSIVE_EXCLUSIVE、Spanned.SPAN_INCLUSIVE_EXCLUSIVE、Spanned.SPAN_EXCLUSIVE_INCLUSIVE、Spanned.SPAN_INCLUSIVE_INCLUSIVE几个参数,INCLUSIVE表示应用该效果,EXCLUSIVE表示不该用该效果,如Spanned.SPAN_INCLUSIVE_EXCLUSIVE表示对前面的文字应用该效果,而对后面的文字不该用该效果。字体

可使用的主要几种Span类型为:spa

ImageSpan 可使用图片替换文字达到图文混排的效果,例如在通常聊天工具当中在文字和表情一块儿发的状态。code

使用方法为:orm

Drawabledrawable=mContext.getResources().getDrawable(R.drawable.new_topic_drawable);

drawable.setBounds(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());

ImageSpan imageSpan = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE);

spanString.setSpan(imageSpan,spanString.length()-1,spanString.length(),Spannable.SPAN_INCLUSIVE_INCLUSIVE);复制代码

ForegroundColorSpan 设置文字前景色,即文字自己的颜色

spanString.setSpan(new ForegroundColorSpan(Color.parseColor("#f74224")), 0,titleText.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);复制代码

AbsoluteSizeSpan 设置文字的绝对大小值

spanString.setSpan(new AbsoluteSizeSpan(11),0,spanString.length(),titleText.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);复制代码

UrlSpan 设置超连接

URLSpan span = new URLSpan("tel:0123456789");

spanString.setSpan(span, 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);复制代码

BackgroundColorSpan 设置文字背景色

BackgroundColorSpan span = new BackgroundColorSpan(Color.YELLOW);

spanString.setSpan(span, 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);复制代码

StyleSpan 字体设置

StyleSpan span = new StyleSpan(Typeface.BOLD_ITALIC);

spanString.setSpan(span, 0, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);复制代码

Typeface中有四个Style常量,分别是BOLD粗体、ITALIC斜体、BOLD_ITALIC粗斜体、NORMAL正常

StrikethroughSpan 删除线

StrikethroughSpan span = new StrikethroughSpan();

spanString.setSpan(span, 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);复制代码

UnderlineSpan下划线

UnderlineSpan span = new UnderlineSpan();

spanString.setSpan(span, 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);复制代码

在具体实践中能够对同一范围内的文字叠加不一样的span,如文字的大小和文字的颜色能够叠加使用,能够组合出不一样的效果

此外在API 17中,TextView自带了几个方法也能够达到图文混排的效果:

public void setCompoundDrawablesRelative (Drawable start, Drawable top, Drawable end, Drawable bottom) Sets the Drawables (if any) to appear to the start of, above, to the end of, and below the text. Use null if you do not want a Drawable there. The Drawables must already have had setBounds(Rect) called. Related XML Attributes android:drawableStart android:drawableTop android:drawableEnd android:drawableBottom public void setCompoundDrawablesRelativeWithIntrinsicBounds (Drawable start, Drawable top, Drawable end, Drawable bottom) Sets the Drawables (if any) to appear to the start of, above, to the end of, and below the text. Use null if you do not want a Drawable there. The Drawables' bounds will be set to their intrinsic bounds. Related XML Attributes android:drawableStart android:drawableTop android:drawableEnd android:drawableBottom public void setCompoundDrawablesRelativeWithIntrinsicBounds (int start, int top, int end, int bottom) Sets the Drawables (if any) to appear to the start of, above, to the end of, and below the text. Use 0 if you do not want a Drawable there. The Drawables' bounds will be set to their intrinsic bounds. Related XML Attributes android:drawableStart android:drawableTop android:drawableEnd android:drawableBottom Parameters start Resource identifier of the start Drawable. top Resource identifier of the top Drawable. end Resource identifier of the end Drawable. bottom Resource identifier of the bottom Drawable.复制代码

因为项目中要达到兼容2.3.x版本的目的,并未使用,读者也能够自行研究如下相关方法的使用