Android TextView style Span Usage Details, androidtextview

Source: Internet
Author: User

Android TextView style Span Usage Details, androidtextview
TextView in Android is a UI class for displaying text. In actual needs, there are various styles for text. TextView itself has no attribute to set and implement, we can use the SpannableString class encapsulation provided by Android. Android provides many Span classes to implement styles. These styles are inherited from CharacterStyle classes. To understand the specific use of Span, you must understand the composition of SPan groups, study the code inheritance structure, and have a deep understanding and understanding to better use it. Let's take a look at the following statistics: URLSpan, ClickableSpan, BackgroundColorSpan, expires, MaskFilterSpan, expires, expires, ImageSpan, ScaleXSpan, StyleSpan, SubscriptSpan, SuperscriptSpan, expires, TypefaceSpan, RasterizerSpan, expires, UnderlineSpan.
Let's take a look at the common constant Spanned in Spanable. SPAN_EXCLUSIVE_EXCLUSIVE --- does not contain the end point (a, B) Spanned of start and end. SPAN_EXCLUSIVE_INCLUSIVE --- does not contain end start, but contains the end (a, B] Spanned. SPAN_INCLUSIVE_EXCLUSIVE --- contains start, but does not include the endpoint [a, B) Spanned. SPAN_INCLUSIVE_INCLUSIVE --- the endpoint where start and end are located [a, B]
:
Usage Details: (the use is very simple, so only a simple explanation, directly on the code, to see the effect) 1. use of SpannableString: Various spans encapsulate styles through SpannableString. After the Span is set, you need to put the Span into the SpannableString class, and then set the SpannableString to TextView. Use: TestText test = (TestText) findViewById (R. id. test); SpannableString spannableString = new SpannableString (testText); BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan (Color. RED); spannableString. setSpan (backgroundColorSpan, 0, 10, Spannable. SPAN_EXCLUSIVE_EXCLUSIVE); test. setText (spannableString );

2. BackgroundColorSpan: The name is related to the background color. Use: BackgroundColorSpan backgroundColorSpan =NewBackgroundColorSpan (Color.RED); SpannableString. setSpan (backgroundColorSpan, 0, 10, Spannable. SPAN_EXCLUSIVE_EXCLUSIVE );

3. ClickableSpan: Click the Span related to the event. Code implementation: ClickableSpan clickableSpan = new ClickableSpan () {@ Override public void onClick (View widget) {Toast. makeText (MainActivity. this, "click", Toast. LENGTH_SHORT ). show (); Log. e ("Easy", "click") ;}}; spannableString. setSpan (clickableSpan, 11, 21, Spannable. SPAN_EXCLUSIVE_INCLUSIVE); test. setMovementMethod (LinkMovementMethod. getInstance ());
Note: When Using ClickableSpan, you must set the MovementMethod object for all actions to be executed when you click a link.
4. URLSpan: Link, similar to the tag in HTML. Code implementation: URLSpan urlSpan =NewURLSpan ("http://www.baidu.com"); spannableString. setSpan (urlSpan, 22, 32, Spannable. SPAN_INCLUSIVE_EXCLUSIVE );
UrilSpan is inherited from ClickableSpan. Let's take a look at the source code: @ Override public void onClick (View widget) {Uri uri = Uri. parse (getURL (); Context context = widget. getContext (); Intent intent = new Intent (Intent. ACTION_VIEW, uri); intent. putExtra (Browser. EXTRA_APPLICATION_ID, context. getPackageName (); context. startActivity (intent );}
URILSpan also implements onClick and redirects the URL to the browser. Therefore, we can use ClickableSpan to perform text click events. Note: When Using URLSpan, you also need to set the MovementMethod object.
5. ForegroundColorSpan: Set the font color. Code implementation: ForegroundColorSpan foregroundColorSpan =NewForegroundColorSpan (Color.GRAY); SpannableString. setSpan (foregroundColorSpan, 32, 43, Spannable. SPAN_INCLUSIVE_INCLUSIVE );


6. MaskFilterSpan: decorative effect of text. There are two types: BlurMaskFilter (Fuzzy effect) and EmbossMaskFilter (embossed effect) MaskFilterSpan maskFilterSpan =NewMaskFilterSpan (NewEmbossMaskFilter (New Float[] {, 3}, 1.5f, 8, 3); spannableString. setSpan (maskFilterSpan, 44, 55, Spannable. SPAN_EXCLUSIVE_EXCLUSIVE); MaskFilter filter2 = new BlurMaskFilter (10, Blur. OUTER); MaskFilterSpan maskFilterSpan2 = new MaskFilterSpan (filter2); spannableString. setSpan (maskFilterSpan2, 56, 67, Spannable. SPAN_EXCLUSIVE_INCLUSIVE );

There are very unfamiliar classes. Let's take a look at the source code: (BlurMaskFilter) public enum Blur {NORMAL (0 ),SOLID(1 ),OUTER(2 ),INNER(3); Blur (int value) {native_int = value;} final int native_int ;}
Public BlurMaskFilter (float radius, Blur style) {native_instance = nativeConstructor (radius, style. native_int);} private static native long nativeConstructor (float radius, int style );

Ah, the native method is called. Remember how to use it first. Blur is its internal class and provides four styles. If you are interested, you can experiment with the results one by one. EmbossMaskFilter is the same method for calling native.
6. AbsoluteSizeSpan: font size code implementation: AbsoluteSizeSpan absoluteSizeSpan =NewAbsoluteSizeSpan (100); spannableString. setSpan (absoluteSizeSpan, 77, 88, Spannable. SPAN_INCLUSIVE_INCLUSIVE); For details, see the source code implementation: @ Override public void updateDrawState (TextPaint ds) {if (mDip) {ds. setTextSize (mSize * ds. density);} else {ds. setTextSize (mSize );}}
It can be seen that the font size of the Set paint, In the constructorPublicAbsoluteSizeSpan (IntSize,BooleanDip) dip indicates whether the data you pass in is a dp value.
7. RelativeSizeSpan: relative font size. What does this mean? You can see the source code: @ Override public void updateDrawState (TextPaint ds) {ds. setTextSize (ds. getTextSize () * mProportion);} How many times is the font size.
8. ImageSpan: image.
Code implementation: ImageSpan imageSpan =NewImageSpan (MainActivity.This, R. drawable.Ic_launcher); SpannableString. setSpan (imageSpan, 100,105, Spannable. SPAN_EXCLUSIVE_EXCLUSIVE );
This class has many constructor functions. It is used to pass in drawble in different ways. It can be a resource address, Drawable, or uri.
9. ScaleXSpan: Scale Factor for horizontal compression. 10. StyleSpan: mainly consists of four styles: Normal, bold, italic, and bold at the same time. The constant value is defined in the Typeface class.

Typeface. DEFAULT // Regular font type Typeface. DEFAULT_BOLD // The Typeface font of the ult_bold type. MONOSPACE // same width font type Typeface. SANS_SERIF // sans serif font type Typeface. SERIF // serif font type

11. SubscriptSpan: The footer style, such as the common writing method of the chemical formula. Of course, you can also scale the text of the footer to see the source code: SubscriptSpan (): No parameter structure.
SubscriptSpan (Parcel src): a parameter structure. The src parameter does not play any role. The source code is:
Public SuperscriptSpan (Parcel src ){
} This is quite special. This constructor has no function.

12. SuperscriptSpan: superscript style, such as the power operation in mathematics. Of course, you can also scale the superscript. The same as the 11 style.
13. TextAppearanceSpan: Use the style file to define text styles.
14. TypefaceSpan: font style. Different fonts can be set.
15. RasterizerSpan: Set the grating.
16. StrikethroughSpan: strikethrough 17. UnderlineSpan: underline

It is easy to use and mainly analyzes the principle of Span. The next article analyzes the deep source code of Span.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.