Preface used in the work, share with you

Reference: http://blog.csdn.net/fengkuanghun/article/details/7904284Copy the code

The difference between SpannableStringBuilder and SpannableString is similar to StringBuilder and String in that SpannableStringBuilder can be spliced and SpannableString cannot be spliced.

Link: www.jianshu.com/p/f004300c6…

Major methods

SpannableStringBuilder and SpannableString mainly change text styles by using setSpan(Object What, int Start, int End, int Flags). Corresponding parameters:

Start: specifies the start position of the Span. End: specifies the end position of the Span. Flags: The values can be four spannable. SPAN_INCLUSIVE_EXCLUSIVE: SPAN_INCLUSIVE_INCLUSIVE: spannable.span_inclusive_inclusive: spannable.span_inclusive_inclusive: spannable.span_inclusive_inclusive: SPAN_EXCLUSIVE_EXCLUSIVE: spannable.SPAN_exclusive_EXCLUSIVE: spannable.SPAN_exclusive_exclusive: spannable.span_exclusive_exclusive: spannable.span_exclusive_exclusive: Does not include the front does not include Spannable. SPAN_EXCLUSIVE_INCLUSIVE: does not include the front, behind, including: the corresponding various Span, different Span corresponding to different styles. Known available classes are: BackgroundColorSpan: text background color ForegroundColorSpan: text color MaskFilterSpan: Modifier effects, such as BlurMaskFilter relief RasterizerSpan: StrikethroughSpan: deletion lines SuggestionSpan: equivalent to placeholder UnderlineSpan: Underline AbsoluteSizeSpan: Text font (absolute size) DynamicDrawableSpan: Sets the picture based on text baseline or bottom alignment. ImageSpan: relative size (text font) ScaleXSpan: Based on the X axis to scale the StyleSpan: bold, italic, etc. SubscriptSpan: TextAppearanceSpan (including font, size, style and color) TypefaceSpan (URLSpan) : Text hyperlink ClickableSpan: Click eventCopy the code

usage

Create a TextView in XML:

<TextView
    android:id="@+id/mode1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="18dp" />Copy the code

SpannableStringBuilder is similar to SpannableString, so here’s an example of SpannableString

/** * Use SpannableString to set styles -- font color */ private voidmode1() {
        SpannableString spannableString = new SpannableString("Shadow IV is already on the rampage.");
        ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.parseColor("#009ad6")); spannableString.setSpan(colorSpan, 0, 8, Spannable.SPAN_EXCLUSIVE_INCLUSIVE); ((TextView)findViewById(R.id.mode1)).setText(spannableString); } When creating SpannableString, pass in the string to display. Use ForegroundColorSpan to set the color for the text. Effect: font colorCopy the code

The following uses SpannableStringBuilder as an example

/** * Use SpannableStringBuilder to set style -- font color */ private voidmode2() {
        SpannableStringBuilder spannableString = new SpannableStringBuilder();
        spannableString.append("Shadow IV");
        spannableString.append("Already on the rampage.");
        ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.parseColor("#009ad6")); spannableString.setSpan(colorSpan, 0, 8, Spannable.SPAN_EXCLUSIVE_INCLUSIVE); ((TextView)findViewById(R.id.mode2)).setText(spannableString); } Here you can see the concatenability of SpannableStringBuilder, which also uses ForegroundColorSpan to set the color of the text. /** * Use SpannableStringBuilder to set styles -- background color */ private voidmode3() {
        SpannableStringBuilder spannableString = new SpannableStringBuilder();
        spannableString.append("Shadow IV is already on the rampage.");
        BackgroundColorSpan bgColorSpan = new BackgroundColorSpan(Color.parseColor("#009ad6"));
        spannableString.setSpan(bgColorSpan, 0, 8, Spannable.SPAN_EXCLUSIVE_INCLUSIVE); ((TextView)findViewById(R.id.mode3)).setText(spannableString); } use BackgroundColorSpan to set the background color. /** * Use SpannableStringBuilder to set the style -- font size */ private voidmode4() {
        SpannableStringBuilder spannableString = new SpannableStringBuilder();
        spannableString.append("Shadow IV is already on the rampage."); AbsoluteSizeSpan absoluteSizeSpan = new AbsoluteSizeSpan(20); spannableString.setSpan(absoluteSizeSpan, 0, 8, Spannable.SPAN_EXCLUSIVE_INCLUSIVE); ((TextView)findViewById(R.id.mode4)).setText(spannableString); } Set font size using AbsoluteSizeSpan. /** * Use SpannableStringBuilder to set the style -- bold/italic */ private voidmode5() {
        SpannableStringBuilder spannableString = new SpannableStringBuilder();
        spannableString.append("Shadow IV is already on the rampage.");
        //setSpan Can be used multiple times in StyleSpan StyleSpan = new StyleSpan(typeface.bold); Spannablestring. setSpan(styleSpan, 0, 3, spannable. SPAN_EXCLUSIVE_INCLUSIVE); StyleSpan styleSpan2 = new StyleSpan(Typeface.ITALIC); // Italic spannableString.setSpan(styleSpan2, 3, 6, spannable. SPAN_EXCLUSIVE_INCLUSIVE); StyleSpan styleSpan3 = new StyleSpan(Typeface.BOLD_ITALIC); Spannablestring. setSpan(styleSpan3, 6, 9, spannable. SPAN_EXCLUSIVE_INCLUSIVE); ((TextView)findViewById(R.id.mode5)).setText(spannableString); } Bold/italic strikeout /** * Use SpannableStringBuilder to set the style -- strikeout */ private voidmode6() {
        SpannableStringBuilder spannableString = new SpannableStringBuilder();
        spannableString.append("Shadow IV is already on the rampage."); StrikethroughSpan strikethroughSpan = new StrikethroughSpan(); spannableString.setSpan(strikethroughSpan, 0, 8, Spannable.SPAN_EXCLUSIVE_INCLUSIVE); ((TextView)findViewById(R.id.mode6)).setText(spannableString); } StrikethroughSpa uses StrikethroughSpan to set the StrikethroughSpan. Delete line underline /** * Use SpannableStringBuilder to set styles -- underline */ private voidmode7() {
        SpannableStringBuilder spannableString = new SpannableStringBuilder();
        spannableString.append("Shadow IV is already on the rampage."); UnderlineSpan underlineSpan = new UnderlineSpan(); spannableString.setSpan(underlineSpan, 0, 8, Spannable.SPAN_EXCLUSIVE_INCLUSIVE); ((TextView)findViewById(R.id.mode7)).setText(spannableString); } use UnderlineSpan to set the underline. Underline not only supports text styles, but also inserts images. SpannableStringBuilder~~ /** * Use SpannableStringBuilder to set the style -- picture */ private voidmode8() {
        SpannableStringBuilder spannableString = new SpannableStringBuilder();
        spannableString.append("Shadow IV is already on the rampage."); ImageSpan imageSpan = new ImageSpan(this, R.mipmap.ic_launcher); //Drawable Drawable = getResources().getDrawable(r.map.ic_launcher); //drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); //ImageSpan imageSpan1 = new ImageSpan(drawable); Spannablestring. setSpan(imageSpan, 6, 8, spannable. SPAN_EXCLUSIVE_INCLUSIVE); ((TextView)findViewById(R.id.mode8)).setText(spannableString); } Set the image with ImageSpan to replace the index characters 6 and 7 with the image, that is, the image occupies the position of index6 and 7. Click event insert picture already very perverted, incredibly still support click event. /** * Use SpannableStringBuilder to set click events */ private voidmode9() {
        SpannableStringBuilder spannableString = new SpannableStringBuilder();
        spannableString.append("Shadow IV is already on the rampage.");
        ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this, "Please don't ask me.", Toast.LENGTH_SHORT).show(); }}; spannableString.setSpan(clickableSpan, 5, 8, Spannable.SPAN_EXCLUSIVE_INCLUSIVE); TextView textView = (TextView)findViewById(R.id.mode9); textView.setText(spannableString); textView.setMovementMethod(LinkMovementMethod.getInstance()); } click event using ClickableSpan Settings, finally also need to add textView. SetMovementMethod (LinkMovementMethod. GetInstance ()); . The characters that specify index 5, 6, and 7 become clickable text; the rest of the code remains unclickable. After clicking on the event, you also need to set the whole TextView to a separate click event. What question do you want to know? Here is a solution I saw on CSDN to solve this problem from another Angle. -- Solution combination using /** * SpannableStringBuilder event combination using */ private voidmode10() {
        SpannableStringBuilder spannableString = new SpannableStringBuilder();
        spannableString.append("Shadow IV is already on the rampage."); // ImageSpan ImageSpan = new ImageSpan(this, r.map.ic_launcher); spannableString.setSpan(imageSpan, 2, 4, Spannable.SPAN_EXCLUSIVE_INCLUSIVE); ClickableSpan = newClickableSpan() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this, "Please don't ask me.", Toast.LENGTH_SHORT).show(); }}; spannableString.setSpan(clickableSpan, 2, 4, Spannable.SPAN_EXCLUSIVE_INCLUSIVE); // ForegroundColorSpan colorSpan = new ForegroundColorSpan(color.parsecolor ()"#FFFFFF")); spannableString.setSpan(colorSpan,5, 8, Spannable.SPAN_EXCLUSIVE_INCLUSIVE); // Text background color BackgroundColorSpanbgColorSpan = new BackgroundColorSpan(Color.parseColor("#009ad6"));
        spannableString.setSpan(bgColorSpan, 5, 8, Spannable.SPAN_EXCLUSIVE_INCLUSIVE); TextView textView = (TextView)findViewById(R.id.mode10); textView.setText(spannableString); textView.setMovementMethod(LinkMovementMethod.getInstance()); } ImageSpan, ClickableSpan, ForegroundColorSpan, BackgroundColorSpan are used in combination, and you can use them according to your own needs.Copy the code