[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)