简介 看下类的英文说明:
This is the class for text whose content is immutable but to which markup objects can be attached and detached. For mutable text, see {@link SpannableStringBuilder}.
装饰物可以添加和删除的定长文本。针对不定长文本,请使用SpannableStringBuilder。
类源码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 public class SpannableString extends SpannableStringInternal implements CharSequence , GetChars , Spannable { public SpannableString (CharSequence source) { super (source, 0 , source.length()); } private SpannableString (CharSequence source, int start, int end) { super (source, start, end); } public static SpannableString valueOf (CharSequence source) { if (source instanceof SpannableString) { return (SpannableString) source; } else { return new SpannableString(source); } } public void setSpan (Object what, int start, int end, int flags) { super .setSpan(what, start, end, flags); } public void removeSpan (Object what) { super .removeSpan(what); } public final CharSequence subSequence (int start, int end) { return new SpannableString(this , start, end); } }
这个类本身是没有多少内容的,但是如果深究到其父类,可能还是有一些东西可以看到。 整体来说,你可以把他理解成一个高阶的String。SpannableStringBuilder就是高阶的StringBuilder.
基本使用 看下frameworks/base/core/java/android/text/style/目录下的文件内容,大部分是支持的Span种类。
AbsoluteSizeSpan 单位为物理像素
1 2 3 4 5 SpannableString spanString = new SpannableString("AbsoluteSizeSpan" ); AbsoluteSizeSpan absoluteSizeSpan = new AbsoluteSizeSpan(100 ); spanString.setSpan(absoluteSizeSpan, 2 , 5 , Spannable.SPAN_INCLUSIVE_INCLUSIVE); mAbsoluteSizeSpanTextView.setText(spanString);
AlignmentSpan 支持ALIGN_NORMAL,ALIGN_OPPOSITE,ALIGN_CENTER,效果自行替换尝试
1 2 3 4 5 6 7 SpannableString spannableString2 = new SpannableString( "AlignmentSpanasdfasdfasdfasdfasdfasdasdfasdfasdasdagsdfsdfasdfsdfasdfasdafsdfsdfsdfsdf" ); AlignmentSpan alignmentSpan = new AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER); spannableString2.setSpan(alignmentSpan, 0 , spannableString2.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE); mAlignmentSpanTextView.setText(spannableString2);
BackgroundColorSpan 文字背景色改变
1 2 3 4 5 6 SpannableString spannableString3 = new SpannableString("BackgroundColorSpan" ); BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan( Color.parseColor("#FFAACC" )); spannableString3.setSpan(backgroundColorSpan, 2 , 5 , Spanned.SPAN_INCLUSIVE_INCLUSIVE); mBackgroundColorSpanTextView.setText(spannableString3);
BulletSpan 1 2 3 4 5 SpannableString spannableString4 = new SpannableString("BulletSpan" ); spannableString4.setSpan(new BulletSpan(66 , 0xff303F9F ), 0 , spannableString4.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE); mBulletSpanTextView.setText(spannableString4);
ClickableSpan 1 2 3 4 5 6 7 8 9 10 SpannableString spannableString5 = new SpannableString("ClickableSpan" ); spannableString5.setSpan(new ClickableSpan() { @Override public void onClick (View view) { Toast.makeText(MainActivity.this , "Click" , Toast.LENGTH_SHORT).show(); } }, 0 , 5 , Spanned.SPAN_INCLUSIVE_INCLUSIVE); mClickableSpanTextView.setText(spannableString5); mClickableSpanTextView.setMovementMethod(LinkMovementMethod.getInstance());
DrawableMarginSpan 1 2 3 4 5 6 SpannableString spannableString6 = new SpannableString("DrawableMarginSpan" ); DrawableMarginSpan drawableMarginSpan = new DrawableMarginSpan( getResources().getDrawable(R.drawable.android), 10 ); spannableString6.setSpan(drawableMarginSpan, 0 , 0 , Spanned.SPAN_INCLUSIVE_INCLUSIVE); mDrawableMarginSpanTextView.setText(spannableString6);
DynamicDrawableSpan 1 2 3 4 5 6 7 8 9 10 11 12 SpannableString spannableString7 = new SpannableString("DynamicDrawableSpan" ); DynamicDrawableSpan dynamicDrawableSpan = new DynamicDrawableSpan() { @Override public Drawable getDrawable () { Drawable drawable = getResources().getDrawable(R.drawable.android); drawable.setBounds(0 , 0 , 100 , 100 ); return drawable; } }; spannableString7.setSpan(dynamicDrawableSpan, 0 , 4 , Spanned.SPAN_INCLUSIVE_INCLUSIVE); mDynamicDrawableSpanTextView.setText(spannableString7);
ForegroundColorSpan 1 2 3 4 5 6 7 SpannableString spannableString9 = new SpannableString("ForegroundColorSpan" ); ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan( Color.parseColor("#AABBCC" )); spannableString9.setSpan(foregroundColorSpan, 1 , spannableString9.length() - 1 , Spanned.SPAN_INCLUSIVE_INCLUSIVE); mForegroundColorSpanTextView.setText(spannableString9);
IconMarginSpan 1 2 3 4 5 6 7 8 SpannableString spannableString10 = new SpannableString( "IconMarginSpan------------------------" ); IconMarginSpan iconMarginSpan = new IconMarginSpan( BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)); spannableString10.setSpan(iconMarginSpan, 0 , spannableString10.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE); mIconMarginSpanTextView.setText(spannableString10);
ImageSpan 1 2 3 4 5 6 7 SpannableString spannableString11 = new SpannableString( "ImageSpan------------------------" ); ImageSpan imageSpan = new ImageSpan(this , BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)); spannableString11.setSpan(imageSpan, 0 , 1 , Spanned.SPAN_INCLUSIVE_INCLUSIVE); mImageSpanTextView.setText(spannableString11);
LeadingMarginSpan 1 2 3 4 5 6 SpannableString spannableString12 = new SpannableString( "LeadingMarginSpan.Standard--------------------------------------------------" ); spannableString12.setSpan(new LeadingMarginSpan.Standard(96 , 36 ), 0 , spannableString12.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE); mLeadingMarginSpanTextView.setText(spannableString12);
QuoteSpan 1 2 3 4 5 SpannableString spannableString15 = new SpannableString("QuoteSpan" ); QuoteSpan quoteSpan = new QuoteSpan(Color.BLACK); spannableString15.setSpan(quoteSpan, 0 , 5 , Spanned.SPAN_INCLUSIVE_INCLUSIVE); mQuoteSpanTextView.setText(spannableString15);
RelativeSizeSpan 1 2 3 4 5 //字体放大 SpannableString spannableString17 = new SpannableString("RelativeSizeSpan"); RelativeSizeSpan relativeSizeSpan = new RelativeSizeSpan(2.5F); spannableString17.setSpan(relativeSizeSpan, 0, 4, Spanned.SPAN_INCLUSIVE_INCLUSIVE); mRelativeSizeSpanTextView.setText(spannableString17);
ScaleXSpan 1 2 3 4 5 SpannableString spannableString18 = new SpannableString("ScaleXSpan" ); ScaleXSpan scaleXSpan = new ScaleXSpan(5 ); spannableString18.setSpan(scaleXSpan, 0 , 4 , Spanned.SPAN_INCLUSIVE_INCLUSIVE); mScaleXSpanTextView.setText(spannableString18);
StrikethroughSpan 1 2 3 4 5 SpannableString spannableString19 = new SpannableString("StrikethroughSpan" ); StrikethroughSpan strikethroughSpan = new StrikethroughSpan(); spannableString19.setSpan(strikethroughSpan, 0 , 4 , Spanned.SPAN_INCLUSIVE_INCLUSIVE); mStrikethroughSpanTextView.setText(spannableString19);
StyleSpan 1 2 3 4 5 6 7 8 9 SpannableString spannableString20 = new SpannableString("StyleSpan" ); Parcel parcel = Parcel.obtain(); parcel.writeInt(Typeface.BOLD_ITALIC); parcel.setDataPosition(0 ); StyleSpan styleSpan = new StyleSpan(parcel); spannableString20.setSpan(styleSpan, 0 , spannableString20.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE); mStyleSpanTextView.setText(spannableString20);
SubscriptSpan 1 2 3 4 5 SpannableString spannableString21 = new SpannableString("SubscriptSpan9" ); SubscriptSpan subscriptSpan = new SubscriptSpan(); spannableString21.setSpan(subscriptSpan, 0 , spannableString21.length()-7 , Spanned.SPAN_INCLUSIVE_INCLUSIVE); mSubscriptSpanTextView.setText(spannableString21);
SuperscriptSpan 1 2 3 4 5 SpannableString spannableString23 = new SpannableString("SuperscriptSpan" ); SuperscriptSpan superscriptSpan = new SuperscriptSpan(); spannableString23.setSpan(superscriptSpan, 0 , spannableString21.length()-7 , Spanned.SPAN_INCLUSIVE_INCLUSIVE); mSuperscriptSpanTextView.setText(spannableString23);
TextAppearanceSpan 1 2 3 4 5 SpannableString spannableString24 = new SpannableString("TextAppearanceSpan" ); TextAppearanceSpan textAppearanceSpan = new TextAppearanceSpan(this , R.style.StyleSpanDemo); spannableString24.setSpan(textAppearanceSpan, 0 , spannableString24.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE); mTextAppearanceSpanTextView.setText(spannableString24);
TypefaceSpan 1 2 3 4 5 SpannableString spannableString25 = new SpannableString("TypefaceSpan" ); TypefaceSpan typefaceSpan= new TypefaceSpan("monospace" ); spannableString25.setSpan(typefaceSpan, 0 , spannableString25.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE); mTypefaceSpanTextView.setText(spannableString25);
UnderlineSpan 1 2 3 4 5 SpannableString spannableString26 = new SpannableString("UnderlineSpan" ); UnderlineSpan underlineSpan= new UnderlineSpan(); spannableString26.setSpan(underlineSpan, 0 , spannableString25.length() - 4 , Spanned.SPAN_INCLUSIVE_INCLUSIVE); mUnderlineSpanTextView.setText(spannableString26);
URLSpan 1 2 3 4 5 SpannableString spannableString27 = new SpannableString("URLSpan------------------------------" ); URLSpan urlSpan = new URLSpan("https://www/baidu.com" ); spannableString27.setSpan(urlSpan, 0 , spannableString26.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE); mURLSpanTextView.setText(spannableString27);
备注 示例完整Github:https://github.com/onlyloveyd/SpannableDemo