49. Various Android Span

Source: Internet
Author: User

49. Various Android Span
49. Various Android Span

Android various Span preface extends URLSpan UnderlineSpan TypefaceSpan extends TabStopSpanStandard SuperscriptSpan SubscriptSpan extends ScaleXSpan StyleSpan extends QuoteSpan MaskFilterSpan extends ImageSpan IconMarginSpan extends BulletSpan BackgroundColorSpan extends ClickableSpan source code

Preface

Inandroid.text.styleUnder the package, there are some Span classes, you canProvides some special content in TextView.. (For example, the color, Font, and size of some content are different, and some fonts can be clicked.)

Another SpannableStringBuilder can help us set the Span.

There are alsoAll source code.

SpannableStringBuilder

SpannableStringBuilderWe can easily set the corresponding Span.

Set Span
SpannableStringBuilder.setSpan(Object what, int start, int end, int flags)

The Flag indicates whether start and end are open or closed intervals.

Flag:

Spanned. SPAN_EXCLUSIVE_EXCLUSIVE --(A, B)

Spanned. SPAN_EXCLUSIVE_INCLUSIVE --(A, B]

Spanned. SPAN_INCLUSIVE_EXCLUSIVE --[A, B)

Spanned. SPAN_INCLUSIVE_INCLUSIVE --[A, B]

URLSpan

Function: Click the text to open a URL.

URLSpan(String url)

SpannableStringBuilder ssb = new SpannableStringBuilder (content); ssb. setSpan (new URLSpan ("https://github.com/CaMnter"), start, sub. length (), Spanned. SPAN_INCLUSIVE_INCLUSIVE); contentTV. setText (ssb); // when you click a link, you must set the MovementMethod object contentTV for all actions to be performed. setMovementMethod (LinkMovementMethod. getInstance (); // sets the color after a click. Here, the ClickableSpan click background contentTV is involved. setHighlightColor (0xff8FABCC );

UnderlineSpan

Function: Underline the text.

UnderlineSpan()

SpannableStringBuilder ssb = new SpannableStringBuilder(content);ssb.setSpan(new UnderlineSpan(), start, start + sub.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);contentTV.setText(ssb);

TypefaceSpan

Function: Set the text font.

TypefaceSpan(String family)The comments in the constructor source code prompt three system fonts:

Monospace

Serif

Sans-serif

    /**     * @param family The font family for this typeface.  Examples include     * "monospace", "serif", and "sans-serif".     */    public TypefaceSpan(String family) {        mFamily = family;    }
SpannableStringBuilder ssb = new SpannableStringBuilder(content);ssb.setSpan(new TypefaceSpan("serif"), start, start + sub.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);contentTV.setText(ssb);

TextAppearanceSpan

Function: Set text font, text style (bold, italic, and so on), text color status, Text underline color status, and so on.

Three construction methods of TextAppearanceSpan

TextAppearanceSpan(Context context, int appearance)

TextAppearanceSpan(Context context, int appearance, int colorList)

TextAppearanceSpan(String family, int style, int size,ColorStateList color, ColorStateList linkColor)

Family:

Monospace

Serif

Sans-serif

Style:

Typeface. NORMAL

Typeface. BOLD

Typeface. ITALIC

Typeface. BOLD_ITALIC

Size: indicates the font size (unit: px)

SpannableStringBuilder ssb = new SpannableStringBuilder(content);ColorStateList colorStateList = null;if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {    colorStateList = this.activity.getColorStateList(R.color.selector_apperarance_span);} else {    try {        colorStateList = ColorStateList.createFromXml(this.activity.getResources(), this.activity.getResources().getXml(R.color.selector_apperarance_span));    } catch (XmlPullParserException | IOException e) {        e.printStackTrace();    }}ssb.setSpan(new TextAppearanceSpan("serif", Typeface.BOLD_ITALIC, this.activity.getResources().getDimensionPixelSize(R.dimen.text_appearance_span), colorStateList, colorStateList), start, start + sub.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);contentTV.setText(ssb);

TabStopSpan. Standard

Function: The offset of the MarginLeft of each line (which is related to \ t and \ n ).

TabStopSpan.Standard(int where)

SpannableStringBuilder ssb = new SpannableStringBuilder (content); String [] subs = content. split (""); ssb = new SpannableStringBuilder ();/*** TabStopSpan. standard related to \ t and \ n * TabStopSpan. standard has a relationship with \ t and \ n */for (String sub1: subs) {ssb. append ("\ t "). append (sub1 ). append (""); ssb. append ("\ n");} ssb. setSpan (new TabStopSpan. standard (126), 0, ssb. length (), Spanned. SPAN_INCLUSIVE_INCLUSIVE); contentTV. setText (ssb );

Vcfnwyw4uu3bmp mrhcmq = "src =" http://www.bkjia.com/uploads/allimg/160101/0413234515-4.png "title =" \ "/>

SuperscriptSpan

Function: Set the text to the upper mark, which is used in mathematical formulas.

SuperscriptSpan(Parcel src)

SpannableStringBuilder ssb = new SpannableStringBuilder(content);ssb.replace(start, start + sub.length(), "Save6");Parcel parcel = Parcel.obtain();parcel.writeInt(6);int sixPosition = ssb.toString().indexOf("6");ssb.setSpan(new SuperscriptSpan(parcel), sixPosition, sixPosition + 1, Spanned.SPAN_INCLUSIVE_INCLUSIVE);parcel.recycle();contentTV.setText(ssb);

SubscriptSpan

Function: The text is set as a subscript, which is used in the chemical formula.

SubscriptSpan(Parcel src)

SpannableStringBuilder ssb = new SpannableStringBuilder(content);ssb.replace(start, start + sub.length(), "Save6");Parcel parcel = Parcel.obtain();parcel.writeInt(6);int sixPosition = ssb.toString().indexOf("6");ssb.setSpan(new SubscriptSpan(parcel), sixPosition, sixPosition + 1, Spanned.SPAN_INCLUSIVE_INCLUSIVE);parcel.recycle();contentTV.setText(ssb);

StrikethroughSpan

Function: Delete text.

StrikethroughSpan()

SpannableStringBuilder ssb = new SpannableStringBuilder(content);ssb.setSpan(new StrikethroughSpan(), start, start + sub.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);contentTV.setText(ssb);

ScaleXSpan

Function: Horizontal text scaling.

ScaleXSpan(float proportion)

Proportion: scaling ratio

SpannableStringBuilder ssb = new SpannableStringBuilder(content);ssb.setSpan(new ScaleXSpan(2.0f), start, start + sub.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);contentTV.setText(ssb);

StyleSpan

Function: Set the text style (normal, bold, italic, bold italic ).

StyleSpan(int style)

Style:

Typeface. NORMAL

Typeface. BOLD

Typeface. ITALIC

Typeface. BOLD_ITALIC

SpannableStringBuilder ssb = new SpannableStringBuilder(content);ssb.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), start, start + sub.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);contentTV.setText(ssb);

RelativeSizeSpan

Function: Sets the relative text size, which is the relative proportion of the text size.

RelativeSizeSpan(float proportion)

Proportion: Size ratio.

SpannableStringBuilder ssb = new SpannableStringBuilder(content);ssb.setSpan(new RelativeSizeSpan(6.0f), start, start + sub.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);contentTV.setText(ssb);

QuoteSpan

Function: Set the reference style (a vertical line) on the left side of the text ).

QuoteSpan(@ColorInt int color)

Color: The color of the vertical line.

SpannableStringBuilder ssb = new SpannableStringBuilder(content);ssb.setSpan(new QuoteSpan(0xff000000), start, start + sub.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);contentTV.setText(ssb);

MaskFilterSpan

Function: Set the text blur effect and relief effect.

MaskFilterSpan(MaskFilter filter)

MaskFilter:

BlurMaskFilter:Blur Effect

EmbossMaskFilter:Embossed Effect

SpannableStringBuilder ssb = new SpannableStringBuilder(content);MaskFilterSpan embossMaskFilterSpan = new MaskFilterSpan(new EmbossMaskFilter(new float[]{3, 3, 9}, 3.0f, 12, 16));ssb.setSpan(embossMaskFilterSpan, start, start + sub.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);contentTV.setText(ssb);String you = "you";int indexYou = content.indexOf(you);MaskFilterSpan blurMaskFilterSpan = new MaskFilterSpan(new BlurMaskFilter(3, BlurMaskFilter.Blur.OUTER));ssb.setSpan(blurMaskFilterSpan, indexYou, indexYou + you.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);contentTV.setText(ssb);

LeadingMarginSpan. Standard

Function: Set text indent.

LeadingMarginSpan.Standard(int first, int rest)

First: the margin left offset of the first line.
Rest: the margin left offset of other rows.

SpannableStringBuilder ssb = new SpannableStringBuilder(content);ssb.append(" ")        .append(ssb.toString())        .append(ssb.toString())        .append(ssb.toString());ssb.setSpan(new LeadingMarginSpan.Standard(96, 36), 0, ssb.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);contentTV.setText(ssb);

ImageSpan

Function: Insert text into an image.

There are many constructor methods:

ImageSpan(Context context, Bitmap b)

ImageSpan(Context context, Bitmap b, int verticalAlignment)

ImageSpan(Drawable d)

ImageSpan(Drawable d, int verticalAlignment)

ImageSpan(Drawable d, String source)

ImageSpan(Drawable d, String source, int verticalAlignment)

ImageSpan(Context context, Uri uri)

ImageSpan(Context context, Uri uri, int verticalAlignment)

ImageSpan(Context context, @DrawableRes int resourceId)

ImageSpan(Context context, @DrawableRes int resourceId, int verticalAlignment)

VerticalAlignment:

ImageSpan. ALIGN_BOTTOM

ImageSpan. ALIGN_BASELINE

Source: The local path String of the image.(Xxx/xxx.jpg)

Uri: The uri of the image.

SpannableStringBuilder ssb = new SpannableStringBuilder(content);ssb.replace(start, start + sub.length(), " Save");ssb.setSpan(new ImageSpan(this.activity, R.mipmap.ic_mm_1, ImageSpan.ALIGN_BASELINE), 0, 1, Spanned.SPAN_INCLUSIVE_INCLUSIVE);contentTV.setText(ssb);

IconMarginSpan

Function: Insert text into image + Margin.

IconMarginSpan(Bitmap b, int pad)

Pad: the offset of margin.

SpannableStringBuilder ssb = new SpannableStringBuilder(content);Bitmap bitmap = BitmapFactory.decodeResource(this.activity.getResources(), R.mipmap.ic_mm_1);ssb.setSpan(new IconMarginSpan(bitmap, 60), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);//bitmap.recycle();contentTV.setText(ssb);

ForegroundColorSpan

Function: Set the text color.

ForegroundColorSpan(@ColorInt int color)

SpannableStringBuilder ssb = new SpannableStringBuilder(content);ssb.setSpan(new ForegroundColorSpan(0xff303F9F), start, start + sub.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);contentTV.setText(ssb);

DrawableMarginSpan

Function: Insert text into image + Margin.

DrawableMarginSpan(Drawable b, int pad)

Pad: the offset of margin.

SpannableStringBuilder ssb = new SpannableStringBuilder(content);ssb.setSpan(new DrawableMarginSpan(ResourcesUtil.getDrawable(this.activity, R.mipmap.ic_mm_1), 6), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);contentTV.setText(ssb);

BulletSpan

Function: Similar

  • The dot effect of the label.

     

    BulletSpan(int gapWidth, int color)

    Gap Width: the gap between the dot and text.

    Color: The dot color.

    SpannableStringBuilder ssb = new SpannableStringBuilder(content);ssb.setSpan(new BulletSpan(66, 0xff303F9F), start, start + sub.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);contentTV.setText(ssb);

    BackgroundColorSpan

    Function: Set the background color.

    BackgroundColorSpan(int color)

    SpannableStringBuilder ssb = new SpannableStringBuilder(content);String you = "you";int indexYou = content.indexOf(you);ssb.setSpan(new BackgroundColorSpan(0x2f303F9F), start, start + sub.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);ssb.setSpan(new BackgroundColorSpan(0x2fFF4081), indexYou, indexYou + you.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);contentTV.setText(ssb);

    AlignmentSpan. Standard

    Function: Set text alignment.

    AlignmentSpan.Standard(Layout.Alignment align)

    Align:

    Layout. Alignment. ALIGN_NORMAL

    Layout. Alignment. ALIGN_OPPOSITE

    Layout. Alignment. ALIGN_CENTER

    Layout. Alignment. ALIGN_LEFT

    Layout. Alignment. ALIGN_RIGHT

    SpannableStringBuilder ssb = new SpannableStringBuilder(content);ssb.setSpan(new AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER), 0, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);contentTV.setText(ssb);
    AbsoluteSizeSpan

    Function: Set the absolute text size.

    AbsoluteSizeSpan(int size, boolean dip)

    Size: the default unit is px.
    Dip: the unit of true to size is dip, and false to px.

    SpannableStringBuilder ssb = new SpannableStringBuilder(content);ssb.setSpan(new AbsoluteSizeSpan(26, true), start, start + sub.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);contentTV.setText(ssb);

    ClickableSpan

    Function: Clickable text.

    Abstract class, which must be extended by yourself.

    ClickableSpanNoUnderline

    Public class ClickableSpanNoUnderline extends ClickableSpan {private static final String TAG = "ClickableSpan"; private static final int NO_COLOR =-206; private int color; private OnClickListener onClickListener; public ClickableSpanNoUnderline (int color, onClickListener onClickListener) {super (); this. color = color; this. onClickListener = onClickListener;} public ClickableSpanNoUnderline (OnCli CkListener onClickListener) {this (NO_COLOR, onClickListener);}/*** Makes the text underlined and in the link color. ** @ param ds */@ Override public void updateDrawState (@ NonNull TextPaint ds) {super. updateDrawState (ds); // set the text color if (this. color = NO_COLOR) {ds. setColor (ds. linkColor);} else {ds. setColor (this. color);} ds. clearShadowLayer (); // remove the underline ds. setUnderlineText (false); ds. bgColor = Color. TRANSPARENT;}/*** Performs the click action associated with this span. ** @ param widget */@ Override public void onClick (View widget) {if (this. onClickListener! = Null) {this. onClickListener. onClick (widget, this);} else {Log. w (TAG, "listener was null") ;}/ *** callback interface, which calls back its onClick event * to indicate whether the external user is clicked */public interface OnClickListener
        
         
    {/*** ClickableSpan is clicked ** @ param widget * @ param span */void onClick (View widget, T span );}}
        

    SpanClickableSpan

    private class SpanClickableSpan extends ClickableSpanNoUnderline {    private String urlString;    public String getUrlString() {        return urlString;    }    public void setUrlString(String urlString) {        this.urlString = urlString;    }    public SpanClickableSpan(int color, OnClickListener onClickListener) {        super(color, onClickListener);    }    public SpanClickableSpan(OnClickListener onClickListener) {        super(onClickListener);    }}

    Start to use

    SpannableStringBuilder ssb = new SpannableStringBuilder (content); SpanClickableSpan spanClickableSpan = new SpanClickableSpan (0xffFF4081, new ClickableSpanNoUnderline. onClickListener () {/*** ClickableSpan is clicked ** @ param widget * @ param span */@ Override public void onClick (View widget, SpanClickableSpan span) {String urlString = span. getUrlString (); if (TextUtils. isEmpty (urlString) return; Uri uri = Uri. parse (urlString); Context context = widget. getContext (); Intent intent = new Intent (Intent. ACTION_VIEW, uri); intent. putExtra (Browser. EXTRA_APPLICATION_ID, context. getPackageName (); try {context. startActivity (intent);} catch (ActivityNotFoundException e) {Log. w ("URLSpan", "Activity was not found for intent," + intent. toString () ;}}); spanClickableSpan. setUrlString ("https://github.com/CaMnter"); ssb. setSpan (spanClickableSpan, start, start + sub. length (), Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); contentTV. setText (ssb); // when you click a link, you must set the MovementMethod object contentTV for all actions to be performed. setMovementMethod (LinkMovementMethod. getInstance (); // sets the color after a click. Here, the ClickableSpan click background contentTV is involved. setHighlightColor (0x00000000 );

    Source code

    You can go to githubNo. 32. Of course, we also need Star, T.

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.