Android learning notes-TextView (text box) (2), android-textview

Source: Internet
Author: User

Android learning notes-TextView (text box) (2), android-textview

Reference: http://www.runoob.com/w3cnote/android-tutorial-textview.html

 

2.4 use autoLink to identify link types

When a URL, email, phone number, or map is displayed in the text, you can set the autoLink attribute. When you click the text in the text, you can jump to a default APP, such as a string of numbers. Click to jump to the dialing interface!

All is included. The protocol header is automatically recognized ~ In Java code, you can call setAutoLinkMask (Linkify. ALL); at this time, you can leave the protocol header unspecified, and autolink will automatically identify it, but you must set setMovementMethod (LinkMovementMethod. getInstance (); otherwise it won't work if you click it!

2.5 TextView HTML

For example, in addition to displaying common text, TextView also predefines some HTML-like labels. With these labels, TextView can display different font colors, sizes, and fonts, even displaying images or links! We only need to use some HTML tags and android. text. HTML class support to complete the above functions!

PS: Of course, not all tags are supported. The following are commonly used:

 
 
  • <Font>: Set the color and font.
  • <Big>: Set the font size.
  • <Small>: Set the font size.
  • <I> <B>: Italic bold
  • <A>: URL
  • <Img>: Image

If setText is used directly, it does not work. We need to call Html. the fromHtml () method converts the string to the CharSequence interface and then sets it. If you need to set it, set it for TextView and call the following method:Java setMovementMethod(LinkMovementMethod.getInstance())

Well, let's write the code to try it:

1) test text and hyperlink labels

Package jay.com. example. textviewdemo; import android. OS. bundle; import android. support. v7.app. appCompatActivity; import android. text. html; import android. text. method. linkMovementMethod; import android. text. util. linkify; import android. widget. textView; public class MainActivity extends AppCompatActivity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); set ContentView (R. layout. activity_main); TextView t1 = (textview1_findviewbyid(r.id.txt One); String s1 = "<font color = 'blue'> <B> Baidu, you will know ~ : </B> </font> <br> "; s1 + =" <a href = 'HTTP: // www.baidu.com '> Baidu </a> "; t1.setText (Html. fromHtml (s1); t1.setMovementMethod (LinkMovementMethod. getInstance ());}}

2) Test the src label and insert an image:

Run the following command:

Next, let's take a look at the implementation code. The implementation code looks a little complicated and uses reflection (by the way, don't forget to place an icon image in the drawable directory !) :

Public class MainActivity extends AppCompatActivity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); TextView t1 = (TextView) findViewById(R.id.txt One); String s1 = "Image:  <br>"; t1.setText (Html. fromHtml (s1, new Html. imageGetter () {@ Override public Drawable getDrawable (String source) {Drawable draw = null; try {Field field = R. drawable. class. getField (source); int resourceId = Integer. parseInt (field. get (null ). toString (); draw = getResources (). getDrawable (resourceId); draw. setBounds (0, 0, draw. getIntrinsicWidth (), draw. getIntrinsicHeight ();} catch (Exception e) {e. printStackTrace () ;}return draw ;}, null ));}}
2.6 SpannableString & SpannableStringBuilder custom text

In addition to the preceding HTML, you can also use the SpannableString and SpannableStringBuilder to customize the TextView style. The difference between the two is that the former is for immutable text, and the latter is for variable Text.

1) The simplest example:Run:

Implementation Code:

Public class MainActivity extends AppCompatActivity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); TextView t1 = (TextView) findViewById(R.id.txt One); TextView t2 = (TextView) blank Two); SpannableString span = new SpannableString ("red phone italic strikethrough green underline image :. "); // 1. set the background color. The flag and Spanned must be specified during setSpan. SPAN_EXCLUSIVE_EXCLUSIVE (not included before and after) span. setSpan (new ForegroundColorSpan (Color. RED), 0, 2, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); // 2. mark the text span with a hyperlink. setSpan (new URLSpan ("tel: 4155551212"), 2, 5, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); // 3. mark text (italic) span with style. setSpan (new StyleSpan (Typeface. BOLD_ITALIC), 5, 7, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); // 4. mark text span with strikethrough. setSpan (new StrikethroughSpan (), 7, 10, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); // 5. mark the text span with an underscore. setSpan (new UnderlineSpan (), 10, 16, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); // 6. mark span with color. setSpan (new ForegroundColorSpan (Color. GREEN), 10, 13, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); // 7. // obtain the Drawable resource Drawable d = getResources (). getDrawable (R. drawable. icon); d. setBounds (0, 0, d. getIntrinsicWidth (), d. getIntrinsicHeight (); // 8. create ImageSpan and use ImageSpan to replace the text ImageSpan imgspan = new ImageSpan (d, ImageSpan. ALIGN_BASELINE); span. setSpan (imgspan, 18, 19, Spannable. SPAN_INCLUSIVE_EXCLUSIVE); t1.setText (span );}}

2) implement some clickable textviewsI believe that friends who have played QQ space and circle of friends are no stranger to the following things. We can click the corresponding user to view the user information, right!

Below we will write a simple example to achieve the following results:

Public class MainActivity extends AppCompatActivity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); TextView t1 = (TextView) findViewById(R.id.txt One); StringBuilder sb = new StringBuilder (); for (int I = 0; I <20; I ++) {sb. append ("friend" + I + ",");} String likeUsers = sb. substring (0, sb. lastIndexOf (",")). ToString (); t1.setMovementMethod (LinkMovementMethod. getInstance (); t1.setText (addClickPart (likeUsers), TextView. bufferType. SPANNABLE);} // defines a processing method for clicking each part of the text. private SpannableStringBuilder addClickPart (String str) {// like the icon. If there is no material, find a smiley face to replace it ~ ImageSpan imgspan = new ImageSpan (MainActivity. this, R. drawable. ic_widget_face); SpannableString spanStr = new SpannableString ("p. "); spanStr. setSpan (imgspan, 0, 1, Spannable. SPAN_INCLUSIVE_EXCLUSIVE); // create a SpannableStringBuilder object to connect multiple strings SpannableStringBuilder ssb = new SpannableStringBuilder (spanStr); ssb. append (str); String [] likeUsers = str. split (","); if (likeUsers. length> 0) {for (int I = 0; I <likeUsers. length; I ++) {final String name = likeUsers [I]; final int start = str. indexOf (name) + spanStr. length (); ssb. setSpan (new ClickableSpan () {@ Override public void onClick (View widget) {Toast. makeText (MainActivity. this, name, Toast. LENGTH_SHORT ). show () ;}@ Override public void updateDrawState (TextPaint ds) {super. updateDrawState (ds); // Delete the underline and set the font color to Blue ds. setColor (Color. BLUE); ds. setUnderlineText (false) ;}}, start, start + name. length (), 0) ;}} return ssb. append ("" + likeUsers. length + "I personally think it is awesome ");}}

Run:

The core is actually: ClickableSpan settings ~

2.7 TextView

Implementation:

Code implementation:

<TextView android: id = "@ + id/txtOne" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: textSize = "18sp" android: singleLine = "true" android: ellipsize = "marquee" android: marqueeRepeatLimit = "marquee_forever" android: focusable = "true" android: focusableInTouchMode = "true" android: text = "you 've been talking about dogs and dogs all day long, but you didn't come ~ "/>
2.8 set TextView font spacing and line spacing

As we usually write documents, we need to typeset and set the spacing between the downstream or words, right? TextView in Android can also be set as follows:

Word Spacing:

Android: textScaleX: Controls horizontal font scaling. The default value is 1.0f and the value is setScaleX (2.0f) in floatJava );

Line spacing:In the Android system, TextView is relatively compact when Chinese characters are displayed by default. In order to keep the row spacing for each row

Android: lineSpacingExtra:Set row spacing, for example, "3dp"Android: lineSpacingMultiplier:Set a line spacing multiple, for example, "1.2"

In Java code, you can use:SetLineSpacingMethod To Set

2.9 automatic line feed

Automatically wrapAndroid: singleLineThe default value is false.

To automatically wrap a line, you can use:

android:singleLine = "false"

If you want to finish displaying a line without line breaks, you can use:

android:singleLine = "true"

In addition, you can also set the number of rows that cannot be displayed. Just add maxLines attributes!

 

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.