TextView is one of the most basic controls in a control, and the simplest one. But if this is the only case, I will not write an article specifically for TextView. A recent discovery of an interesting class in Android is the Spannablestringbuilder written on the title. So what is it? It can add various effects to your text, such as discoloration, various symbols, italic, picture replacement, highlighting and so on. If you want to add a bit of common effect to the text, we don't have to write a custom view on our own, so we can use Spannablestringbuilder to meet your needs.
First, TextView
First is the preparation of TextView, as usual, first write the control in the layout file, and then in the code through the ID to get the reference.
<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical" > <TextViewAndroid:id= "@+id/textview1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/text_view" /> <ButtonAndroid:id= "@+id/button1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/button" /></LinearLayout>
@Override protected void onCreate (Bundle savedinstancestate) { super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); = (TextView) Findviewbyid (r.id.textview1); = (Button) Findviewbyid (r.id.button1); Bt.setonclicklistener (this); }
Second,spannablestringbuilder
Spannablestringbuilder actually speak White is a text editor, by applying different patterns to change the appearance of text, its main method is
/** * Mark The specified range of text with the specified object. * The flags determine how the span would behave when text was * inserted at the start or end of the span ' s range. */public voidint int int. Flags} { Setspan (true, what, start, end, flags); }
The first parameter is a class that implements the Parcelablespan interface;
The 23rd parameter is to change the position of the text, the starting and ending points;
The fourth parameter is used to further restrict and explain the second and third parameters.
Iii. Examples of texts
- Example: changing text by clicking a button
- The code for the key click event is as follows
/** Precautions: Spannablestringbuilder.setspan (NEW * Foregroundcolorspan (color.red), 1, 3, * spannable.span_exc lusive_inclusive); First parameter: Color second parameter: Start position the third parameter: End position * Third parameter: span_exclusive_inclusive. Used to further restrict and describe the second and third parameters * Here the representation does not contain 1, but contains 3. Literal meaning also very well understood */@Override Public voidOnClick (View v) {String str= "Hello cpacm! \na preservation of the memory "; Spannablestringbuilder Builder=NewSpannablestringbuilder (str); //Set ColorBuilder.setspan (NewForegroundcolorspan (color.red), 0, 5, //Setspan required flag,spanned.span_exclusive_exclusive (not included before and after).spanned.span_exclusive_exclusive); //Hypertext LinksBuilder.setspan (NewUrlspan ("https://www.google.com"), 6, 12, spanned.span_exclusive_exclusive); //Bold Italic BodyBuilder.setspan (NewStylespan (Typeface.bold_italic), 12, 15, spanned.span_exclusive_exclusive); //Delete LineBuilder.setspan (NewStrikethroughspan (), 16, 29, spanned.span_exclusive_exclusive); //Picture Replacement textdrawable d =getresources (). getdrawable (R.drawable.ic_launcher); D.setbounds (0, 0, D.getintrinsicwidth (), D.getintrinsicheight ()); //Create ImagespanImagespan span =NewImagespan (d, Imagespan.align_baseline); //Replace text with ImagespanBuilder.setspan (Span, 29, 31, spannable.span_inclusive_exclusive); Builder.setspan (NewForegroundcolorspan (color.red), 31, 36, spannable.span_inclusive_inclusive); //Delete TextBuilder.delete (36, 42); //Add textBuilder.append ("thing"); //give text to TextViewTv.settext (builder); }
- Of course, there are more than one type of span, check the API, there are many types of span, such as.
The usage is the same.
- Run
Iv. Concluding remarks
Ashamed to say, for a long time did not update this series, but also has not had the opportunity to get the Android interface. Today I saw a tool class that changed the text, so I thought about making textview.
========================================
Cpacm
Source: (http://www.cnblogs.com/cpacm/p/4099031.html)
Getting Started with Android's zero-single-row development diary (15)--textview+spannablestringbuilder