Use the android SpannableStringBuilder to implement text-and-image mixing. For more information, see

Source: Internet
Author: User

Use the android SpannableStringBuilder to implement text-and-image mixing. For more information, see

This effect must be achieved in project development


There are two additional rows. The last two rows are the ellipsis.

Previously, I used Html. fromHtml to process text-and-text mixing. It is only the text after the image or text color font or something,

However, you need to add an image after the ellipsis of the last text. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + signature/PC9wPgo8cD48L3A + signature "brush: java;"> 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); if (ellipsizeStr. length () = 16) {TV. getViewTreeObserver (). removeOnGlobalLayoutListener (this);} else {TV. getViewTreeObserver (). removeGlobalOnLayoutListener (this );}}});}
The SpannableStringBuilder Concatenates the omitted text and the final image. You can also splice text at the end,

Don't let... more

Post blog:

Android spannableStringBuilder usage

SpannableStringBuilder Usage Details: SpannableString ss = new SpannableString ("red phone italic strikethrough green underline picture :. "); // mark the text ss with color. setSpan (new ForegroundColorSpan (Color. RED), 0, 2, // the flag, Spanned, To be specified when setSpan is used. SPAN_EXCLUSIVE_EXCLUSIVE (not included before and after ). spanned. SPAN_EXCLUSIVE_EXCLUSIVE); // mark the text ss with a hyperlink. setSpan (new URLSpan ("tel: 4155551212"), 2, 5, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); // use the style to mark the text (italic) ss. setSpan (new StyleSpan (Typeface. BOLD _ ITALIC), 5, 7, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); // mark the text ss with a strikethrough. setSpan (new StrikethroughSpan (), 7, 10, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); // mark the text ss with an underscore. setSpan (new UnderlineSpan (), 10, 16, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); // mark ss with color. setSpan (new ForegroundColorSpan (Color. GREEN), 10, 13, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); // obtain the Drawable resource Drawable d = getResources (). getDrawable (R. drawable. icon); d. SetBounds (0, 0, d. getIntrinsicWidth (), d. getIntrinsicHeight (); // create ImageSpan span = new ImageSpan (d, ImageSpan. ALIGN_BASELINE); // Replace the text ss with ImageSpan. setSpan (span, 18, 19, Spannable. SPAN_INCLUSIVE_EXCLUSIVE); txtInfo. setText (ss); txtInfo. setMovementMethod (LinkMovementMethod. getInstance (); // The scrolling of text is usually used to display text, but sometimes some images need to be included in the text. For example, you can use emoticon images in QQ, for example, how can we achieve this in android? Remember that there is an android. text package in android, which provides powerful text processing functions. To add images, use the SpannableString and ImageSpan classes: Drawable drawable = getResources (). getDrawable (id); drawable. setBounds (0, 0, drawable. getIntrinsicWidth (), drawable. getIntrinsicHeight (); // The text to be processed. [smile] is the text to be replaced by SpannableString spannable = new SpannableString (getText (). toString () + "[smile]"); // you must use ImageSpan span = new ImageSpan (drawable, ImageSpan. ALIGN_BASELINE); // start to replace. Note that parameters 2nd and 3rd indicate where to replace and where to replace (start and end) // The last parameter is similar to a set in mathematics, [5, 12) indicates from 5 to 12, including 5 but not 12 spannable. setSpan (span, getText (). length (), getText (). length () + "[smile]". length (), Spannable. SPAN_INCLUSIVE_EXCLUSIVE); setText (spannable); highlight the required text: public void highlight (int start, int end) {SpannableStringBuilder spannable = new SpannableStringBuilder (getText (). toString (); // used for variable string ForegroundColorSpan span = new ForegroundColorSpan (Color. RED); spannable. setSpan (span, start, end, Spannable. SPAN_EXCLUSIVE_EXCLUSIVE); setText (spannable);} underline: public void underline (int start, int end) {SpannableStringBuilder spannable = new SpannableStringBuilder (getText (). toString (); CharacterStyle span = new UnderlineSpan (); spannable. setSpan (span, start, end, Spannable. SPAN_EXCLUSIVE_EXCLUSIVE); setText (spannable);} Combined Application: 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: you can use this method to display strings with \ n linefeeds in two colors/*** strings with \ n linefeeds can use this method to display two colors * @ param text *@ param color1 * @ param color2 * @ return */public SpannableStringBuilder highlight (String text, int color1, int color2, int fontSize) {SpannableStringBuilder spannable = new SpannableStringBuilder (text); // variable string CharacterStyle span_0 = null, span_1 = null, span_2; int end = text. indexOf ("\ n"); if (end =-1) {// if there is no line break, use the first color to display span_0 = new ForegroundColorSpan (color1); 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_EXCLUSIVE_EXCLUSIVE);} return spannable ;}


If you have any questions, please leave a message and discuss them.

Copy the Translation results to Google

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.