I. Set the color of Some fonts
Pass
Spannablestringbuilder style = new spannablestringbuilder (charsequence CS );
Style. setspan (New foregroundcolorspan (color), begin, end, spannable. span_exclusive_exclusive );
TV. settext (style );
Settings. Due to the complexity of this method, I implemented a helper class to quickly implement the color of Some fonts;
final class TextViewUtil{public static CharSequence setTextColor(CharSequence str,int begin,int end,int color){SpannableStringBuilder style = new SpannableStringBuilder(str);style.setSpan(new ForegroundColorSpan(color), begin, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);return style;} }
Test code:
package org.xiazdong;import android.app.Activity;import android.graphics.Color;import android.os.Bundle;import android.text.Spannable;import android.text.SpannableStringBuilder;import android.text.style.ForegroundColorSpan;import android.widget.TextView;public class MainActivity extends Activity {private TextView tv;@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tv = (TextView)findViewById(R.id.tv); String str = "hello,xiazdong"; CharSequence cq = TextViewUtil.setTextColor(str, 0, 2, Color.RED); cq = TextViewUtil.setTextColor(cq, 2, 5, Color.GREEN); tv.setText(cq); }}
Effect:
Ii. Achieve the running horse light effect (rolling subtitles)
Set the following in <textview> of the main. xml layout file:
android:singleLine="true" android:focusable="true" android:ellipsize="marquee" android:marqueeRepeatLimit="marquee_forever" android:focusableInTouchMode="true"
Effect:
3. Implement hyperchain
Set <textview> as follows:
Android: autolink = "all", if the string in textview is "Hello, xiazdong's blog: http://blog.csdn.net/xiazdong, phone: 12345678901"
The system automatically recognizes http://blog.csdn.net/xiazdongand 12345678901;