Android 神奇的SpannableStringBuilder

Android 神奇的SpannableStringBuilder,第1张

概述一无图言屌先看看神奇的效果仅用一个TextView实现 二SpannableStringBuilderGoogle官方介绍Thisistheclassfortextwhosecontentandmarkupcanbothbechanged.翻译过来这是用于文本的类,其内容和标记都可以更改。继承关系SpannableStringBuilder实现了Cha

一 无图言屌 先看看神奇的效果 仅用一个TextVIEw实现

 

二 SpannableStringBuilder

Google官方介绍

This is the class for text whose content and markup can both be changed.

翻译过来

这是用于文本的类,其内容和标记都可以更改。

继承关系

SpannableStringBuilder实现了CharSequence 因此 可以直接在TextVIEw#setText()中使用

 

三 用法

在xml中创建一个TextVIEw

<TextVIEw    androID:ID="@+ID/text"    androID:layout_wIDth="wrap_content"    androID:layout_height="wrap_content"    androID:layout_margintop="20dp"    androID:layout_marginHorizontal="60dp"    androID:textSize="16sp" />

1. 字体颜色

TextVIEw textVIEw = findVIEwByID(R.ID.text);SpannableStringBuilder builder = new SpannableStringBuilder("0123456789ABCDEFGHIJKLMnopQRSTUVWXYZ");ForegroundcolorSpan span = new ForegroundcolorSpan(color.parsecolor("#0094FF"));builder.setSpan(span, 0, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); //位置含头不含尾textVIEw.setText(builder);

2. 背景颜色

TextVIEw textVIEw = findVIEwByID(R.ID.text);SpannableStringBuilder builder = new SpannableStringBuilder("0123456789ABCDEFGHIJKLMnopQRSTUVWXYZ");BackgroundcolorSpan span = new BackgroundcolorSpan(color.parsecolor("#0094FF"));builder.setSpan(span, 0, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); //位置含头不含尾textVIEw.setText(builder);

3. 字体大小 绝对

TextVIEw textVIEw = findVIEwByID(R.ID.text);SpannableStringBuilder builder = new SpannableStringBuilder("0123456789ABCDEFGHIJKLMnopQRSTUVWXYZ");absoluteSizeSpan span = new absoluteSizeSpan(ScreenUtils.sp2px(20));builder.setSpan(span, 0, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); //位置含头不含尾textVIEw.setText(builder);

4. 字体大小 相对

TextVIEw textVIEw = findVIEwByID(R.ID.text);SpannableStringBuilder builder = new SpannableStringBuilder("0123456789ABCDEFGHIJKLMnopQRSTUVWXYZ");relativeSizeSpan span = new relativeSizeSpan(0.5F);builder.setSpan(span, 0, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); //位置含头不含尾textVIEw.setText(builder);

5. 字体风格

@H_404_85@

TextVIEw textVIEw = findVIEwByID(R.ID.text);SpannableStringBuilder builder = new SpannableStringBuilder("0123456789ABCDEFGHIJKLMnopQRSTUVWXYZ");StyleSpan span = new StyleSpan(Typeface.BolD); //Typeface.BolD=粗体 Typeface.ITAliC=斜体 Typeface.BolD_ITAliC=粗斜体builder.setSpan(span, 0, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); //位置含头不含尾textVIEw.setText(builder);

6. 删除线

TextVIEw textVIEw = findVIEwByID(R.ID.text);SpannableStringBuilder builder = new SpannableStringBuilder("0123456789ABCDEFGHIJKLMnopQRSTUVWXYZ");StrikethroughSpan span = new StrikethroughSpan();builder.setSpan(span, 0, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); //位置含头不含尾textVIEw.setText(builder);

7. 下划线

TextVIEw textVIEw = findVIEwByID(R.ID.text);SpannableStringBuilder builder = new SpannableStringBuilder("0123456789ABCDEFGHIJKLMnopQRSTUVWXYZ");Underlinespan span = new Underlinespan();builder.setSpan(span, 0, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); //位置含头不含尾textVIEw.setText(builder);

8. 图片 原始大小

TextVIEw textVIEw = findVIEwByID(R.ID.text);SpannableStringBuilder builder = new SpannableStringBuilder("0123456789ABCDEFGHIJKLMnopQRSTUVWXYZ");ImageSpan span = new ImageSpan(this, R.mipmap.ic_launcher);builder.setSpan(span, 0, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); //位置含头不含尾textVIEw.setText(builder);

9. 图片 控制大小

TextVIEw textVIEw = findVIEwByID(R.ID.text);SpannableStringBuilder builder = new SpannableStringBuilder("0123456789ABCDEFGHIJKLMnopQRSTUVWXYZ");Drawable drawable = getResources().getDrawable(R.mipmap.ic_launcher);drawable.setBounds(0, 0, ScreenUtils.dp2px(32), ScreenUtils.dp2px(16));ImageSpan span = new ImageSpan(drawable);builder.setSpan(span, 0, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); //位置含头不含尾textVIEw.setText(builder);

10. 点击事件

TextVIEw textVIEw = findVIEwByID(R.ID.text);SpannableStringBuilder builder = new SpannableStringBuilder("0123456789ABCDEFGHIJKLMnopQRSTUVWXYZ");ClickableSpan span = new ClickableSpan() {    @OverrIDe    public voID onClick(@NonNull VIEw vIEw) {        Toast.makeText(vIEw.getContext(), "onClick", Toast.LENGTH_LONG).show();    }    @OverrIDe    public voID updateDrawState(@NonNull TextPaint ds) {        super.updateDrawState(ds);        ds.setcolor(color.parsecolor("#0094FF"));        ds.setUnderlineText(true); //设置下划线        ds.setFakeBoldText(true); //设置粗体    }};builder.setSpan(span, 0, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); //位置含头不含尾textVIEw.setText(builder);// 去除点击时的背景色textVIEw.setHighlightcolor(ContextCompat.getcolor(textVIEw.getContext(), androID.R.color.transparent));// 点击事件生效textVIEw.setMovementMethod(linkMovementMethod.getInstance());

11. 点击事件+图片

TextVIEw textVIEw = findVIEwByID(R.ID.text);SpannableStringBuilder builder = new SpannableStringBuilder("0123456789ABCDEFGHIJKLMnopQRSTUVWXYZ");ClickableSpan span = new ClickableSpan() {    @OverrIDe    public voID onClick(@NonNull VIEw vIEw) {        Toast.makeText(vIEw.getContext(), "onClick", Toast.LENGTH_LONG).show();    }    @OverrIDe    public voID updateDrawState(@NonNull TextPaint ds) {        super.updateDrawState(ds);        ds.setcolor(color.parsecolor("#0094FF"));        ds.setUnderlineText(true); //设置下划线        ds.setFakeBoldText(true); //设置粗体    }};builder.setSpan(span, 0, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); //位置含头不含尾ImageSpan imageSpan = new ImageSpan(this, R.mipmap.ic_launcher);builder.setSpan(imageSpan, 5, 6, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); //位置含头不含尾textVIEw.setText(builder);// 去除点击时的背景色textVIEw.setHighlightcolor(ContextCompat.getcolor(textVIEw.getContext(), androID.R.color.transparent));// 点击事件生效textVIEw.setMovementMethod(linkMovementMethod.getInstance());

 

总结

以上是内存溢出为你收集整理的Android 神奇的SpannableStringBuilder全部内容,希望文章能够帮你解决Android 神奇的SpannableStringBuilder所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1046693.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存