SpannableStringBuilder achieve gorgeous turned TextView

Foreword

Want TextViewthe text to add some highlighted text, then tap to jump, the first thought is TextViewspliced, but considering the display after the new line is not very suitable, after some inquiry found SpannableStringBuilderthat class, not only to achieve the desired effect, can also be more magical, magical exactly how do we look at renderings:

Renderings

Each line is just a TextView, not only can highlight text, and pictures can be inserted, and click to set part of the event, which is SpannableStringBuilderto achieve results, let's look at the details SpannableStringBuilderof this amazing class and its usage.

Brief introduction

Google's official explanation is:

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

After the translation is: this is the type of content and markup can change the text.

In a nutshell is: can we TextViewdisplayed Stringadd a variety of styles, such as: color, size, strikeout, italic, or even a picture, so as to achieve the purpose of highlighting, but also can add individual style while clicking, click achieve different feedback.

SpannableStringBuilderThere is a similar class SpannableString, similar to the difference StringBuilder, Stringis that SpannableStringBuilderyou can use appendmethods splicing, and SpannableStringnot, needs to be initialized when the incoming string.

Method Detail

Method ct what, int start, int end, int flags) `, labeled with the specified range of the specified text object.

Parameter Description:

  1. what: StringDifferent of Span, i.e. different styles available classes comprising:
  • BackgroundColorSpan : Text Background Color
  • ForegroundColorSpan : Text Color
  • MaskFilterSpan: Modifying effects such as blur ( BlurMaskFilter), relief
  • RasterizerSpan : Raster Effects
  • StrikethroughSpan : Strikethrough
  • SuggestionSpan : The equivalent of placeholders
  • UnderlineSpan : Underline
  • AbsoluteSizeSpan : Text (absolute size)
  • DynamicDrawableSpan : Set pictures, text alignment based on baseline or bottom.
  • ImageSpan : Pictures
  • RelativeSizeSpan : Relative size (Text font)
  • ScaleXSpan : Based on the x-axis scaling
  • StyleSpan : Font style: bold, italic, etc.
  • SubscriptSpan : Subscript (mathematical formula will be used)
  • SuperscriptSpan : Superscript (mathematical formula will be used)
  • TextAppearanceSpan : Text appearance (including font, size, style and color)
  • TypefaceSpan : Text Fonts
  • URLSpan : Text hyperlink
  • ClickableSpan : Click Event
  1. start: identifies the Stringstarting position Span style;

  2. end: identifying Stringthe end position Span style;

  3. flags: used to control the behavior, as opening and closing zone, it is usually set to 0 or a constant Spanned defined in commonly used

Spannable.SPAN_EXCLUSIVE_EXCLUSIVE: Do not include front and rear;
Spannable.SPAN_EXCLUSIVE_INCLUSIVE: does not include the front, back comprising;
Spannable.SPAN_INCLUSIVE_EXCLUSIVE: comprising front, back not including;
Spannable.SPAN_INCLUSIVE_INCLUSIVE: both before and after included.

More flag argument can click to see the Android developer documentation SpannableStringBuilder .

Examples of use

First, define and initialize a TextViewcontrol, create SpannableStringBuilder, passing the string contents to be displayed, set the SpannableStringparameters content, by setTextmethods displayed.

(1) font colorForegroundColorSpan

SpannableStringBuilder spanString01 = new SpannableStringBuilder("风急天高猿啸哀");
ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.RED);
spanString01.setSpan(foregroundColorSpan, 4, 7, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
mTvAdvanced01.setText(spanString01);

(2) the font background colorBackgroundColorSpan

SpannableStringBuilder spanString02 = new SpannableStringBuilder("渚清沙白鸟飞回");
BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan(Color.YELLOW);
spanString02.setSpan(backgroundColorSpan, 4, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
mTvAdvanced02.setText(spanString02);

(3) font sizeAbsoluteSizeSpan

SpannableStringBuilder spanString03 = new SpannableStringBuilder("无边落木萧萧下");
AbsoluteSizeSpan absoluteSizeSpan = new AbsoluteSizeSpan(30);
spanString03.setSpan(absoluteSizeSpan, 4, 7, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
mTvAdvanced03.setText(spanString03);

(4) bold, italicStyleSpan

SpannableStringBuilder spanString04 = new SpannableStringBuilder("不尽长江滚滚来");
StyleSpan styleSpan = new StyleSpan(Typeface.BOLD_ITALIC);
spanString04.setSpan(styleSpan, 4, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
mTvAdvanced04.setText(spanString04);

(5) strikethroughStrikethroughSpan

SpannableStringBuilder spanString05 = new SpannableStringBuilder("万里悲秋常作客");
StrikethroughSpan strikethroughSpan = new StrikethroughSpan();
spanString05.setSpan(strikethroughSpan, 4, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
mTvAdvanced05.setText(spanString05);

(6) underlinedUnderlineSpan

SpannableStringBuilder spanString06 = new SpannableStringBuilder("百年多病独登台");
UnderlineSpan underlineSpan = new UnderlineSpan();
spanString06.setSpan(underlineSpan, 4, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
mTvAdvanced06.setText(spanString06);

(7) PicturesImageSpan

SpannableStringBuilder spanString07 = new SpannableStringBuilder("艰难苦恨繁霜鬓");
ImageSpan span = new ImageSpan(this, R.drawable.ic_text);
spanString07.setSpan(span, 4, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
mTvAdvanced07.setText(spanString07);

(8) Click eventClickableSpan

SpannableStringBuilder spanString08 = new SpannableStringBuilder("潦倒新停浊酒杯");
ClickableSpan clickableSpan = new ClickableSpan() {
    @Override
    public void onClick(@NonNull View widget) {
        Toast.makeText(AdvancedTextViewActivity.this, "杜甫", Toast.LENGTH_SHORT).show();
    }
};
spanString08.setSpan(clickableSpan, 4, 7, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
mTvAdvanced08.setText(spanString08);
mTvAdvanced08.setMovementMethod(LinkMovementMethod.getInstance());

(9) SpannableStringBuildera combination of

SpannableStringBuilder spanString09 = new SpannableStringBuilder();
spanString09.append("杜甫 ,字子美,自号少陵野老,后人称为'诗圣'。");
//图片
ImageSpan imageSpan = new ImageSpan(this, R.drawable.ic_author);
spanString09.setSpan(imageSpan, 2, 4, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
//点击事件
ClickableSpan cbSpan = new ClickableSpan() {
    @Override
    public void onClick(@NonNull View widget) {
        Toast.makeText(AdvancedTextViewActivity.this, "杜甫", Toast.LENGTH_SHORT).show();
    }
};
spanString09.setSpan(cbSpan, 2, 4, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
//文字颜色
ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.WHITE);
spanString09.setSpan(colorSpan, 10, 14, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
//文字背景颜色
BackgroundColorSpan bgColorSpan = new BackgroundColorSpan(Color.BLUE);
spanString09.setSpan(bgColorSpan, 10, 14, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
mTvAdvanced09.setText(spanString09);
mTvAdvanced09.setMovementMethod(LinkMovementMethod.getInstance());

On the introduction and use of SpannableStringBuilder on here, and I've uploaded to the example Github development record
, please click to view and Star, I will continue to add other useful knowledge and examples on the project.

Welcome thumbs up / comments, because you agree / encouragement is the greatest power of my writing!

Welcome to public concern number: rings a few times to see more interesting technologies, tools, gossip, resources.
No public .png

Released six original articles · won praise 2 · Views 793

Guess you like

Origin blog.csdn.net/nianlun1/article/details/103963349