在设置textview的各种属性的时候会用到 spannable 很简单 直接看代码
package com.example.login;
import java.net.URL;
import org.xml.sax.XMLReader;
import android.app.Activity;
import android.graphics.BlurMaskFilter;
import android.graphics.BlurMaskFilter.Blur;
import android.graphics.Color;
import android.graphics.EmbossMaskFilter;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.text.Editable;
import android.text.Html;
import android.text.Html.ImageGetter;
import android.text.Html.TagHandler;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.BackgroundColorSpan;
import android.text.style.ClickableSpan;
import android.text.style.DynamicDrawableSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.text.style.MaskFilterSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.ScaleXSpan;
import android.text.style.StrikethroughSpan;
import android.text.style.StyleSpan;
import android.text.style.SubscriptSpan;
import android.text.style.SuperscriptSpan;
import android.text.style.TextAppearanceSpan;
import android.text.style.TypefaceSpan;
import android.text.style.URLSpan;
import android.text.style.UnderlineSpan;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class FromWidgetActivity extends Activity {
private static int mIndex = 1;
private TextView tv;
private TextView tv1;
private TextView tv2;
@Override
protected void onCreate(Bundle arg0) {
// TODO Auto-generated method stub
super.onCreate(arg0);
setContentView(R.layout.fromwidget);
// 这句话是让 在主线程 下载图片 变为可能,没有这句话就报错。。反正我的报错
if (Build.VERSION.SDK_INT >= 11) {
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads().detectDiskWrites().detectNetwork()
.penaltyLog().build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
.detectLeakedSqlLiteObjects().detectLeakedSqlLiteObjects()
.penaltyLog().penaltyDeath().build());
}
tv = (TextView) findViewById(R.id.tv);
tv1 = (TextView) findViewById(R.id.tv1);
tv2 = (TextView) findViewById(R.id.tv2);
showView_span();
}
private void showView_span() {
SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(
"今天工作不努力 明天努力找工作,今天工作不努力 明天努力找工作,今天工作不努力 明天努力找工作," +
"今天工作不努力 明天努力找工作");
// 参数 1 传入对象 2 起始位置 3 重点位置 4 插入的方式
// spannableStringBuilder.setSpan(what, start, end, flags)
// Spanned.SPAN_EXCLUSIVE_EXCLUSIVE --- 不包含两端start和end所在的端点 (a,b)
// Spanned.SPAN_EXCLUSIVE_INCLUSIVE --- 不包含端start,但包含end所在的端点 (a,b]
// Spanned.SPAN_INCLUSIVE_EXCLUSIVE --- 包含两端start,但不包含end所在的端点 [a,b)
// Spanned.SPAN_INCLUSIVE_INCLUSIVE--- 包含两端start和end所在的端点 [a,b]
//
// 但实际测试这其中似乎并未有差别,而在start和end相同的情况下,则只对start所在字符的当前行起作用。
// 1、BackgroundColorSpan 背景色 可以是图片也可以是颜色
BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan(
Color.GREEN);
spannableStringBuilder.setSpan(backgroundColorSpan, 0, 3,
Spannable.SPAN_INCLUSIVE_INCLUSIVE);
// 2 图片
ImageSpan imageSpan = new ImageSpan(FromWidgetActivity.this,
R.drawable.common_input_box_clear);
spannableStringBuilder.setSpan(imageSpan, 3, 4,
Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
// 3、ForegroundColorSpan 文本颜色(前景色)
ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(
Color.GREEN);
spannableStringBuilder.setSpan(foregroundColorSpan, 4, 7,
Spannable.SPAN_INCLUSIVE_INCLUSIVE);
// 4 MaskFilterSpan 修饰效果,如模糊(BlurMaskFilter)、浮雕(EmbossMaskFilter)
// 模糊(BlurMaskFilter)
MaskFilterSpan maskFilterSpan = new MaskFilterSpan(new BlurMaskFilter(
3, Blur.INNER));
spannableStringBuilder.setSpan(maskFilterSpan, 7, 11,
Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
// 浮雕(EmbossMaskFilter)
maskFilterSpan = new MaskFilterSpan(new EmbossMaskFilter(new float[] {
1, 1, 3 }, 1.5f, 8, 3));
spannableStringBuilder.setSpan(maskFilterSpan, 11, 15,
Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
// 5 StrikethroughSpan 删除线(中划线)
spannableStringBuilder.setSpan(new StrikethroughSpan(), 15, 17,
Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
// 6UnderlineSpan 下划线
spannableStringBuilder.setSpan(new UnderlineSpan(), 17, 19,
Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
// 7AbsoluteSizeSpan 绝对大小(文本字体)
spannableStringBuilder.setSpan(new AbsoluteSizeSpan(20, true), 19, 21,
Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
// 8DynamicDrawableSpan 设置图片,基于文本基线或底部对齐。左边图片基于基线对齐,右边图片基于底部对齐
DynamicDrawableSpan drawableSpan = new DynamicDrawableSpan(
DynamicDrawableSpan.ALIGN_BASELINE) {
@Override
public Drawable getDrawable() {
Drawable d = getResources().getDrawable(R.drawable.ic_launcher);
d.setBounds(0, 0, 50, 50);
return d;
}
};
DynamicDrawableSpan drawableSpan2 = new DynamicDrawableSpan(
DynamicDrawableSpan.ALIGN_BOTTOM) {
@Override
public Drawable getDrawable() {
Drawable d = getResources().getDrawable(R.drawable.ic_launcher);
d.setBounds(0, 0, 50, 50);
return d;
}
};
spannableStringBuilder.setSpan(drawableSpan, 21, 22,
Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
spannableStringBuilder.setSpan(drawableSpan2, 24, 25,
Spannable.SPAN_INCLUSIVE_INCLUSIVE);
// 9RelativeSizeSpan 相对大小(文本字体)
RelativeSizeSpan relativeSizeSpan = new RelativeSizeSpan(2f);
spannableStringBuilder.setSpan(relativeSizeSpan, 25, 27,
Spannable.SPAN_INCLUSIVE_INCLUSIVE);
// 10ScaleXSpan 基于x轴缩放
// 参数proportion:比例大小
spannableStringBuilder.setSpan(new ScaleXSpan(2f), 27, 29,
Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
// 11、StyleSpan 字体样式:粗体、斜体等
// Typeface.BOLD_ITALIC:粗体+斜体
spannableStringBuilder.setSpan(new StyleSpan(Typeface.BOLD), 29, 31,
Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
// 12SubscriptSpan 下标(数学公式会用到)
spannableStringBuilder.setSpan(new SuperscriptSpan(), 31, 32,
Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
// 13 SubscriptSpan 上标(数学公式会用到)
spannableStringBuilder.setSpan(new SubscriptSpan(), 33, 34,
Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
// 14TextAppearanceSpan 文本外貌(包括字体、大小、样式和颜色)
// 若需自定义TextAppearance,可以在系统样式上进行修改 PS:系统还提供了相关值TextAppearance_Small,
// TextAppearance_Large等。如有需要可在以上样式基础上修改。
spannableStringBuilder.setSpan(new TextAppearanceSpan(this,
android.R.style.TextAppearance_Medium), 34, 37,
Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
// 15TypefaceSpan 文本字体
//若需使用自定义字体,可能要重写类TypefaceSpan
spannableStringBuilder.setSpan(new TypefaceSpan("SANS_SERIF"), 37, 40,
Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
//没有效果 不知为什么
//16 URLSpan 文本超链接
spannableStringBuilder.setSpan(new URLSpan("http://www.baidu.com"), 40, 43,
Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
//让URLSpan可以点击
tv.setMovementMethod(new LinkMovementMethod());
//17 ClickableSpan 点击事件
ClickableSpan clickableSpan=new ClickableSpan() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(FromWidgetActivity.this, "点击事件", 1).show();
}
};
spannableStringBuilder.setSpan(clickableSpan, 43, 45,
Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
//18 ClickableSpan 点击事件 去下划线
ClickableSpan clickSpan = new NoLineClickSpan(getApplicationContext()); //设置超链接
spannableStringBuilder.setSpan(clickSpan, 47, 50, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
// tv.setMovementMethod(LinkMovementMethod.getInstance());
tv.setText(spannableStringBuilder);
}}
去下划线的代码
package com.example.login;
import android.content.Context;
import android.text.TextPaint;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.Toast;
public class NoLineClickSpan extends ClickableSpan {
// String text;
Context context;
public NoLineClickSpan(Context context) {
super();
// this.text = text;
this.context = context;
}
@Override
public void updateDrawState(TextPaint ds) {
ds.setColor(ds.linkColor);
ds.setUnderlineText(false); //去掉下划线</span>
}
@Override
public void onClick(View widget) {
// processHyperLinkClick(text); //点击超链接时调用</span>
Toast.makeText(context, "123", 1).show();
}
}