android如何设置多个span_Android中实现不同文字颜色和图文混排的Span总结

本文介绍了在Android中如何实现TextView的首行缩进、图文混排以及文字的多种样式设置,包括颜色、大小、超链接、删除线等。通过SpannableString和各种Span类型,如ImageSpan、ForegroundColorSpan、BackgroundColorSpan等,可以创建复杂的文本布局效果。
摘要由CSDN通过智能技术生成

一、怎么在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= newImageSpan(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 = newBackgroundColorSpan(Color.YELLOW);

spanString.setSpan(span,0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

StyleSpan 字体设置

StyleSpan span = newStyleSpan(Typeface.BOLD_ITALIC);

spanString.setSpan(span,0, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

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

StrikethroughSpan 删除线

StrikethroughSpan span = newStrikethroughSpan();

spanString.setSpan(span,0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

UnderlineSpan下划线

UnderlineSpan span = newUnderlineSpan();

spanString.setSpan(span,0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

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

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

public voidsetCompoundDrawablesRelative (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 donot want a Drawable there. The Drawables must already have had setBounds(Rect) called.

Related XML Attributes

android:drawableStart

android:drawableTop

android:drawableEnd

android:drawableBottompublic voidsetCompoundDrawablesRelativeWithIntrinsicBounds (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:drawableBottompublic void setCompoundDrawablesRelativeWithIntrinsicBounds (int start, int top, int end, intbottom)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版本的目的,并未使用,读者也可以自行研究以下相关方法的使用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值