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 41 42 43 44 45 46 47 48 49
| SpannableString spannableString = new SpannableString("前景色背景色相对大小删除线下划线" + "上标小上标下标粗体斜体显示图片点击超链接");
ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.parseColor("#0099EE")); BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan(Color.parseColor("#AC00FF30")); RelativeSizeSpan relativeSizeSpan = new RelativeSizeSpan(2f); StrikethroughSpan strikethroughSpan = new StrikethroughSpan(); UnderlineSpan underlineSpan = new UnderlineSpan(); SuperscriptSpan superscriptSpan = new SuperscriptSpan(); RelativeSizeSpan relativeSizeSpan2 = new RelativeSizeSpan(0.5f); SubscriptSpan subscriptSpan = new SubscriptSpan(); StyleSpan styleSpan_B = new StyleSpan(Typeface.BOLD); StyleSpan styleSpan_I = new StyleSpan(Typeface.ITALIC);
ImageSpan imageSpan = new ImageSpan(this, R.mipmap.ic_launcher); ClickableSpan clickableSpan = new ClickableSpan() { @Override public void onClick(View widget) { Toast.makeText(SpannableActivity.this, "点击", Toast.LENGTH_SHORT).show(); }
@Override public void updateDrawState(TextPaint ds) { ds.setUnderlineText(false); } }; URLSpan urlSpan = new URLSpan("http://www.sdwfqin.com");
spannableString.setSpan(foregroundColorSpan, 0, 3, Spanned.SPAN_INCLUSIVE_EXCLUSIVE); spannableString.setSpan(backgroundColorSpan, 3, 6, Spanned.SPAN_INCLUSIVE_EXCLUSIVE); spannableString.setSpan(relativeSizeSpan, 6, 10, Spanned.SPAN_INCLUSIVE_EXCLUSIVE); spannableString.setSpan(strikethroughSpan, 10, 13, Spanned.SPAN_INCLUSIVE_EXCLUSIVE); spannableString.setSpan(underlineSpan, 13, 16, Spanned.SPAN_INCLUSIVE_EXCLUSIVE); spannableString.setSpan(superscriptSpan, 16, 21, Spanned.SPAN_INCLUSIVE_EXCLUSIVE); spannableString.setSpan(relativeSizeSpan2, 18, 21, Spanned.SPAN_INCLUSIVE_EXCLUSIVE); spannableString.setSpan(subscriptSpan, 21, 23, Spanned.SPAN_INCLUSIVE_EXCLUSIVE); spannableString.setSpan(styleSpan_B, 23, 25, Spanned.SPAN_INCLUSIVE_EXCLUSIVE); spannableString.setSpan(styleSpan_I, 25, 27, Spanned.SPAN_INCLUSIVE_EXCLUSIVE); spannableString.setSpan(imageSpan, 29, 31, Spanned.SPAN_INCLUSIVE_EXCLUSIVE); spannableString.setSpan(clickableSpan, 31, 33, Spanned.SPAN_INCLUSIVE_EXCLUSIVE); spannableString.setSpan(urlSpan, 33, 36, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
spanString.setMovementMethod(LinkMovementMethod.getInstance());
spanString.setText(spannableString);
|