Android Project Combat (a): Spannablestring and Spannablestringbuilder

Source: Internet
Author: User

Original: Android Project Combat (a): Spannablestring and Spannablestringbuilder

Objective:

Once in some apps like "help" "about" the interface to see a line of text display different color effect, the following effect:

The software is a high , large , on the community-class software.

In general, this should be a textview to show, but they will not be realized, how to do it, only a color to make a textview together, forming the above effect.

But it's too low-level for that to happen. Until I accidentally knew the Spannablestring class. Online learning a bit, quite simple a class, the online detailed introduction a lot of, oneself will no longer test write Bo, direct to a write good to save later project use it.

This article goes from:

http://blog.csdn.net/harvic880925/article/details/38984705

I. Overview

1. Relationship between Spannablestring, Spannablestringbuilder and string

First, spannablestring and Spannablestringbuilder are basically similar to string, but they are also used to store strings, but the two are unique in that they have a setspan () function, You can add various formats or styles (spans) to these stored strings, displaying the original string in a different style, such as underlining the original string, adding a background color, changing the font color, replacing the specified text with a picture, and so on. So, all in all, Spannablestring, Spannablestringbuilder and string are the first to pass strings, but spannablestring, Spannablestringbuilder can add additional style information to these strings, but not string.

Note: If this additional information can be supported in the way it is used, for example by passing spannablestring to TextView, there is no support for this additional information, such as the canvas drawing text mentioned in the previous chapter, for unsupported scenarios, Spannablestring and Spannablestringbuilder are degenerate to string types, displaying the original string string directly, without any additional additional information being displayed.

2. Difference between spannablestring and Spannablestringbuilder

The difference is that spannablestring, like a string, constructs an object by passing in a string, then cannot change the contents of the string, and cannot stitch multiple spannablestring While Spannablestringbuilder is more like a StringBuilder, it can stitch multiple strings through its append () method:

[Java]View Plaincopy
    1. Use spannablestring, must pass in once, construct completes
    2. Spannablestring Word = new spannablestring ("Welcome to Harvic's blog");
    3. With Spannablestringbuilder, you can use append () to add
    4. Spannablestringbuilder Multiword = new Spannablestringbuilder ();
    5. Multiword.append ("Welcome");
    6. Multiword.append ("Harvic");
    7. Multiword.append ("blog");


(Go to Blog: integer and string substitution in android-spannablestring or Spannablestringbuilder and string.xml files)

Because Spannable and so on finally implemented the Charsequence interface, so you can directly spannablestring and spannablestringbuilder through the Textview.settext () set to TextView.

3, Setspan ()

void Setspan (Object what, int start, int end, int flags)

function meaning: Set the span style for spannablestring or spannablestringbuilder a specific range of strings, you can set multiple (for example, underline and strikethrough), The FALG parameter identifies whether the same style is applied to the newly inserted character when the new character is inserted before and after the marked range. (This will be explained in detail later in this example)

Parameter description:

Object What: corresponds to the various spans, which will be mentioned later;
int start: Start applying the location of the specified span, starting with index 0
int end: Ends the position where the specified span is applied, and the effect does not include this position. For example, if the number is 3 (that is, the 4th character), the 4th character will have no effect. It can be seen from the example below.
INT flags: There are four of the following values
Spannable.span_exclusive_exclusive: None before and after, that is, inserting new characters before and after a specified range does not apply a new style
Spannable.span_exclusive_inclusive: Not included before, included later. That is, the new style is applied only when a new character is inserted after the range character
Spannable.span_inclusive_exclusive: Included earlier, not included later.
Spannable.span_inclusive_inclusive: Both front and back are included.

Let's take an example to illustrate the issues that are included:

Since flag is used to specify the range of characters to be entered before and after the new character, it will not apply the effect, so we use EditText to display spannablestring

(1), add a EditText control to the layout xml:

[HTML]View Plaincopy
  1. <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="Http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. tools:context="Com.example.try_spannable_blog. Mainactivity " >
  6. <EditText
  7. android:id="@+id/edit"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content" />
  10. </relativelayout>

(2), here with a change font color of span to do the next demonstration

[Java]View Plaincopy
  1. Public class Mainactivity extends Activity {
  2. private EditText EditText;
  3. @Override
  4. protected void OnCreate (Bundle savedinstancestate) {
  5. super.oncreate (savedinstancestate);
  6. Setcontentview (R.layout.activity_main);
  7. EditText = (editText) Findviewbyid (R.id.edit);
  8. //Change font color
  9. //construct spannablestring first
  10. Spannablestring spanstring = new spannablestring ("Welcome to Harvic's blog");
  11. //Construct a span that changes the font color
  12. Foregroundcolorspan span = new Foregroundcolorspan (Color.Blue);
  13. //Apply this span to the specified range of fonts
  14. Spanstring.setspan (span, 1, 3, spannable.span_exclusive_inclusive);
  15. //Set to EditText display
  16. Edittext.settext (spanstring);
  17. }
  18. }

The initialization effect is this:

Add new text before and after setting span, as a result

In front and back are added to the shrimp two words, visible, the front of the shrimp did not have any effect, the latter is different, add the same SPAN effect, this is because we set up the reason for the spannable.span_exclusive_inclusive, that is, (the front does not apply effects, After the application of special effects), a few other flags of the meaning of the parameters must be clear to everyone. We will not dwell on it here.

Ii. various span settings

In the previous small example, you should also be able to see that there are three steps to apply a span in total:
1. Construct string
2. Construct span
3. Use Setspan () to apply this span to a specified range of strings

1. Font Color setting (Foregroundcolorspan)

[Java]View Plaincopy
    1. Spannablestring spanstring = new spannablestring ("Welcome to Harvic's blog");
    2. and construct a span that changes the color of the font.
    3. Foregroundcolorspan span = new Foregroundcolorspan (Color.Blue);
    4. Apply this span to the specified range of fonts
    5. Spanstring.setspan (span, 1, 5, spannable.span_exclusive_inclusive);
    6. Set it to edittext display
    7. Edittext.settext (spanstring);

Effect:

2. Font background color (backgroundcolorspan)

[Java]View Plaincopy
    1. Spannablestring spanstring = new spannablestring ("Welcome to Harvic's blog");
    2. Backgroundcolorspan span = new Backgroundcolorspan (Color.yellow);
    3. Spanstring.setspan (span, 0, 3, spannable.span_exclusive_exclusive);
    4. Edittext.settext (spanstring);

3. Font size (Absolutesizespan)

[Java]View Plaincopy
    1. Spannablestring spanstring = new spannablestring ("Welcome to Harvic's blog");
    2. Absolutesizespan span = new Absolutesizespan (16);
    3. Spanstring.setspan (span, 2, 5, spannable.span_inclusive_inclusive);
    4. Edittext.settext (spanstring);

4, Bold, italic (Stylespan)

[Java]View Plaincopy
    1. Spannablestring spanstring = new spannablestring ("Welcome to Harvic's blog");
    2. Stylespan span = new Stylespan (typeface.bold_italic);
    3. Spanstring.setspan (span, 1, 4, spannable.span_exclusive_exclusive);
    4. Edittext.settext (spanstring);

5. Delete line (Strikethroughspan)

[Java]View Plaincopy
    1. Spannablestring spanstring = new spannablestring ("Welcome to Harvic's blog");
    2. Strikethroughspan span = new Strikethroughspan ();
    3. Spanstring.setspan (span, 2, 5, spannable.span_exclusive_exclusive);
    4. Edittext.settext (spanstring);

6, Underline (Underlinespan)

[Java]View Plaincopy
    1. Spannablestring spanstring = new spannablestring ("Welcome to Harvic's blog");
    2. Underlinespan span = new Underlinespan ();
    3. Spanstring.setspan (span, 1, 4, spannable.span_exclusive_exclusive);
    4. Edittext.settext (spanstring);



7, picture replacement (Imagespan)

Imagspan has a lot of constructors, which are usually constructed by passing in the Drawableg, and the detailed construction instructions look here: http://developer.android.com/reference/android/text/style/ImageSpan.html

[Java]View Plaincopy
    1. spannablestring spanstring =  New spannablestring ( "Welcome to Harvic's blog");     
    2. Drawable d = getresources (). getdrawable (r.drawable.ic_launcher);     
    3. Li class= "alt" >d.setbounds ( 0, 0, d.getintrinsicwidth (),  d.getintrinsicheight ());     
    4. imagespan span = < span class= "keyword" >new imagespan (d, imagespan.align_baseline);     
    5. Li class= "alt" >spanstring.setspan (Span,  2, 4,  spannable.span_exclusive_exclusive);     
    6. edittext.settext (spanstring );    


The difference between this function is that the first few are based on the original text with special effects, and here is the use of images to replace the text. If you encounter a function that does not support displaying a picture, such as a canvas drawing in the previous article. Will degenerate into a string, which is displayed in the original string string.

This article relates to the pictures and works: http://download.csdn.net/detail/harvic880925/7854761

Please respect the original copyright, reproduced please indicate the source: http://blog.csdn.net/harvic880925/article/details/38984705 Thank you!

Android Project Combat (a): Spannablestring and Spannablestringbuilder

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.