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

1,701 阅读3分钟

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

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

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

二、TextView中的图文混排和不同颜色、大小字体的显示

方法一:设置不同颜色、大小、图文混排的效果通过SpannableString,并且设置各种Span实现的。

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类型为:

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

使用方法为:

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版本的目的,并未使用,读者也可以自行研究以下相关方法的使用