Use Android Spannablestringbuilder to achieve picture and text mix, see more

Source: Internet
Author: User

This effect needs to be achieved in project development


Two more lines, two lines at the end of the ellipsis, and the ellipsis followed by the drop-down more

Previously used is html.fromhtml to deal with the picture and text mixed, just the words after the image or text color font or something,

But here, you need to add a picture after the ellipsis of the last text.

Directly on the code bar, code comments A lot, slowly study

private void Toggleellipsize (final TextView tv,final String desc) {if (desc = = null) {return;} Tv.getviewtreeobserver (). Addongloballayoutlistener (New Ongloballayoutlistener () {@Overridepublic void Ongloballayout () {Boolean isellipsized = (Tv.gettag () ==null| | Tv.gettag (). Equals (False)) false: (Boolean) Tv.gettag (); if (isellipsized) {Tv.settag (false); Tv.settext (desc);} Else{tv.settag (true); int paddingleft = Tv.getpaddingleft (); int paddingright = Tv.getpaddingright (); Textpaint paint = Tv.getpaint (); Float MoreText = Tv.gettextsize () *3;float availabletextwidth = (Tv.getwidth ()- Paddingleft-paddingright) *2-moretext; Charsequence ellipsizestr = textutils.ellipsize (desc,paint,availabletextwidth,textutils.truncateat.end); Ellipsizestr.length () <desc.length ()) {/*string html = "=16) {tv.getviewtreeobserver (). RemoveongloballayoutlisteneR (this);} Else{tv.getviewtreeobserver (). Removeglobalonlayoutlistener (This);}});}

Mainly by Spannablestringbuilder the omitted text and the final picture to the stitching up. Can also be the final stitching is the text,

Not to let ... more

Goto Blog:

Android Spannablestringbuilder Usage Collation

Spannablestringbuilder usage explained: spannablestring SS = new Spannablestring ("Red call italic strikethrough green underline Picture:."); Color-coded text Ss.setspan (new Foregroundcolorspan (color.red), 0, 2,//setspan need to specify the Flag,spanned.span                 _exclusive_exclusive (not included before and after).         spanned.span_exclusive_exclusive);         Tagged text with hyperlinks ss.setspan (new Urlspan ("tel:4155551212"), 2, 5, spanned.span_exclusive_exclusive); Mark text with Style (italic) Ss.setspan (new Stylespan (Typeface.bold_italic), 5, 7, Spanned.span_exclusiv         e_exclusive);         Mark text with Strikethrough Ss.setspan (new Strikethroughspan (), 7, ten, spanned.span_exclusive_exclusive);         Mark text with underline Ss.setspan (new Underlinespan (), ten, +, spanned.span_exclusive_exclusive); Color-coded Ss.setspan (new Foregroundcolorspan (Color.green), ten, Spanned.span_exclusive_exclusi         VE); Get drawable resource drawable d = GetresoUrces (). getdrawable (R.drawable.icon);         D.setbounds (0, 0, d.getintrinsicwidth (), D.getintrinsicheight ());         Create Imagespan Imagespan span = new Imagespan (d, Imagespan.align_baseline);           Replace text with Imagespan (span, Ss.setspan, spannable.span_inclusive_exclusive);         Txtinfo.settext (ss); Txtinfo.setmovementmethod (Linkmovementmethod.getinstance ()); The implementation of text scrolling is usually used to display text, but sometimes it is necessary to include some pictures in the text, such as QQ can use emoticons, such as the need for text highlighting and so on, how to do so in Android? Remember that there is a Android.text package in Android, which provides powerful handling of the text.          Add pictures primarily with spannablestring and Imagespan classes: drawable drawable = Getresources (). getdrawable (ID);          Drawable.setbounds (0, 0, drawable.getintrinsicwidth (), Drawable.getintrinsicheight ());          The text that needs to be processed, [smile] is the text that needs to be substituted spannablestring spannable = new Spannablestring (GetText (). toString () + "[Smile]");          To replace the specified text with a picture, use imagespan imagespan span = new Imagespan (drawable, imagespan.align_baseline); Start replacing, note that the 2nd and 3rd parameters represent where to start replacing where to replace the end (Start and end)///The last parameter is similar to a set in mathematics, [5,12] represents from 5 to 12, including 5 but excluding the Spannable.setspan (span, GetText (). Length (), GetText ().            Length () + "[smile]". Length (), spannable.span_inclusive_exclusive);   SetText (spannable); Highlight required text: public void highlight (int start,int end) {Spannablestringbuilder spannable=new Spannablestringbuil          Der (GetText (). toString ());//For variable string foregroundcolorspan span=new Foregroundcolorspan (color.red);          Spannable.setspan (span, start, end, spannable.span_exclusive_exclusive);      SetText (spannable); Add underscore: public void underline (int start,int end) {Spannablestringbuilder spannable=new Spannablestringbuil          Der (GetText (). toString ());          Characterstyle span=new Underlinespan ();          Spannable.setspan (span, start, end, spannable.span_exclusive_exclusive);      SetText (spannable);          The combination applies: Spannablestringbuilder spannable=new Spannablestringbuilder (GetText (). toString ()); Characterstyle Span_1=new Stylespan (Android.graphics.Typeface.ITALIC);          Characterstyle span_2=new Foregroundcolorspan (color.red);          Spannable.setspan (span_1, start, end, spannable.span_exclusive_exclusive);          Spannable.setspan (span_2, start, end, spannable.span_exclusive_exclusive);     SetText (spannable); Case: a string with \ n newline characters can display 2 colors in this method/** * A string with \ n newline characters can display 2 colors in this method * @param text * @param color1 * @para M Color2 * @return * * Public Spannablestringbuilder Highlight (String text,int color1,int color2,int fontsiz e) {Spannablestringbuilder spannable=new spannablestringbuilder (text);//For variable string Characterstyle span_0=nu          ll,span_1=null,span_2;          int End=text.indexof ("\ n");              if (end==-1) {//Use the first color to display span_0=new Foregroundcolorspan (color1) If there is no line break;          Spannable.setspan (span_0, 0, Text.length (), spannable.span_exclusive_exclusive); }else{span_0=new Foregroundcolorspan (cOlor1);              Span_1=new Foregroundcolorspan (COLOR2);              Spannable.setspan (span_0, 0, end, spannable.span_exclusive_exclusive);                            Spannable.setspan (span_1, End+1, Text.length (), spannable.span_exclusive_exclusive); Span_2=new Absolutesizespan (fontSize);//font size Spannable.setspan (span_2, End+1, Text.length (), spannable.span_e          xclusive_exclusive);      } return spannable; }


In doubt, you can leave a message, exchange discussions

Copy to Google TranslateTranslation Results

Use Android Spannablestringbuilder to achieve picture and text mix, see more

Related Article

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.