Android TextView combined with SpannableString

Source: Internet
Author: User

Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. textview );
TextView txtInfo = (TextView) findViewById (R. id. TV );
// SpannableString text class, which contains immutable text but can be replaced and separated with existing objects.
// For variable Text classes, refer to SpannableStringBuilder.
SpannableString ss = new SpannableString ("red phone italic strikethrough green underline image :.");
// Mark text with color
Ss. setSpan (new ForegroundColorSpan (Color. RED), 0, 2,
// The flag to be specified during setSpan, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE (not included before and after ).
Spanned. SPAN_EXCLUSIVE_EXCLUSIVE );
// Mark text with hyperlink
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_EXCLUSIVE_EXCLUSIVE );
// Mark text with strikethrough
Ss. setSpan (new StrikethroughSpan (), 7, 10,
Spanned. SPAN_EXCLUSIVE_EXCLUSIVE );
// Mark the text with underscores
Ss. setSpan (new UnderlineSpan (), 10, 16,
Spanned. SPAN_EXCLUSIVE_EXCLUSIVE );
// Mark with color
Ss. 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
ImageSpan span = new ImageSpan (d, ImageSpan. ALIGN_BASELINE );
// Replace text with ImageSpan
Ss. setSpan (span, 18, 19, Spannable. SPAN_INCLUSIVE_EXCLUSIVE );
TxtInfo. setText (ss );
TxtInfo. setMovementMethod (LinkMovementMethod. getInstance (); // implement text scrolling.
}

:

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.