/**
* 设置前景色
* @param tv
*/
private void setForegroundColorSpan(TextView tv) {
SpannableString spanString = new SpannableString("前景色textview");
ForegroundColorSpan span = new ForegroundColorSpan(Color.BLUE);
spanString.setSpan(span, 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.append(spanString);
}
/**
* 设置背景色
* @param tv
*/
private void setBackgroundColorSpan(TextView tv) {
SpannableString spanString = new SpannableString("背景色textview");
BackgroundColorSpan span = new BackgroundColorSpan(Color.YELLOW);
spanString.setSpan(span, 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.append(spanString);
}
/**
* 设置粗体样式
* @param tv
*/
private void setStyleSpan(TextView tv) {
SpannableString spanString = new SpannableString("粗体斜体textview");
StyleSpan span = new StyleSpan(Typeface.BOLD_ITALIC);
spanString.setSpan(span, 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.append(spanString);
}
/**
* 设置字体大小
* @param tv
*/
private void setRelativeFontSpan(TextView tv) {
SpannableString spanString = new SpannableString("字体相对大小textview");
spanString.setSpan(new RelativeSizeSpan(2.5f), 0, 6,Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
tv.append(spanString);
}
/**
* 设置文本字体
* @param tv
*/
private void setTypefaceSpan(TextView tv) {
SpannableString spanString = new SpannableString("文本字体textview");
spanString.setSpan(new TypefaceSpan("monospace"), 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.append(spanString);
}
/**
* 添加URL超链接
* @param tv
*/
private void addUrlSpan(TextView tv) {
SpannableString spanString = new SpannableString("csdntextview");
URLSpan span = new URLSpan("http://blog.csdn.net/nuptboyzhb");
spanString.setSpan(span, 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.append(spanString);
tv.setMovementMethod(LinkMovementMethod.getInstance());
}
/**
* 在文字中显示图片表情等
* @param tv
*/
private void addImageSpan(TextView tv) {
SpannableString spanString = new SpannableString("文中有图片表情等textview");
Drawable d = getResources().getDrawable(R.drawable.ic_launcher);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
spanString.setSpan(span, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.append(spanString);
}
/**
* 点击后跳转Activity
* @param textView
* @param packageContext
* @param cls
*/
private void clickableSpan(TextView textView,final Context packageContext,final Class<?> cls){
String text = "显示Activity";
SpannableString spannableString = new SpannableString(text);
spannableString.setSpan(new ClickableSpan() {
@Override
public void onClick(View widget) {
Intent intent = new Intent(packageContext,cls);
startActivity(intent);
}
// 表示点击整个text的长度都有效触发这个事件
}, 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);
textView.setMovementMethod(LinkMovementMethod.getInstance());
}
/**
* 添加下划线
* @param tv
*/
private void addUnderLineSpan(TextView tv) {
SpannableString spanString = new SpannableString("下划线textview");
UnderlineSpan span = new UnderlineSpan();
spanString.setSpan(span, 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.append(spanString);
}
/**
* 添加删除线样式
* @param tv
*/
private void addStrikeSpan(TextView tv) {
SpannableString spanString = new SpannableString("删除线textview");
StrikethroughSpan span = new StrikethroughSpan();
spanString.setSpan(span, 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.append(spanString);
}