[Android development diary in a row from scratch] (15th) -- TextView + SpannableStringBuilder, androidtextview

Source: Internet
Author: User

[Android development diary in a row from scratch] (15th) -- TextView + SpannableStringBuilder, androidtextview

TextView is the most basic and simplest control in a control. However, I will not write an article for TextView. Recently I found an interesting class in Android, namely the SpannableStringBuilder written on the title. So what is it? It can add various effects to your text, such as discoloration, symbols, italics, image replacement, and highlight. If you want to add some common effects to text, you do not have to write a custom View by yourself.SpannableStringBuilderYou can meet your needs.

 

I. TextView

The first step is to prepare TextView. As usual, write the control in the layout file first, and then get the reference through the id in the code.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    >    <TextView        android:id="@+id/textView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/text_view" />    <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/button" /></LinearLayout>

 

@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);        tv = (TextView)findViewById(R.id.textView1);    bt = (Button)findViewById(R.id.button1);    bt.setOnClickListener(this);    }

II,SpannableStringBuilder

SpannableStringBuilder is actually a text editor, which uses different modes to change the text. The main method is

    /**     * Mark the specified range of text with the specified object.     * The flags determine how the span will behave when text is     * inserted at the start or end of the span's range.     */    public void setSpan(Object what, int start, int end, int flags) {        setSpan(true, what, start, end, flags);    }

 

The first parameter is a class that implements the ParcelableSpan interface;

The second three parameters are used to change the position, start point, and end point of the text;

The fourth parameter is used to further limit and describe the second and third parameters.

 

III,Text Example

 

IV,Conclusion

Sorry, I haven't updated this series for a long time, which is also the reason why I have never had the chance to get the Android interface. Today, I saw a tool class with text changes, so I thought about making TextView.

==========================================================

 

Author: cpacm
(Http://www.cnblogs.com/cpacm/p/4099031.html)

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.