TextView算是android开发中最最常用的控件了,有的时候,我们要给一个TextView中的显示的文字设置不同的样式或者响应事件,比如同一个TextView中,有的字是红色,有的字是蓝色,有的字点击之后有响应事件,有的点击之后没有响应事件,甚至我们想在TextView中显示一个数学公式等等,那么对于形形色色的需求我们有没有解决方案呢?当然有,一种是使用HTML来解决,另一种就是使用SpannableString,HTML比较简单,本文主要介绍后者。SpannableString可以用来显示复合文本,我们可以通过SpannableString给文本设置各种各样的样式,下面我们就来看看SpannableString的一些常见用法。
tv1 = (TextView) this.findViewById(R.id.tv1); SpannableString ss1 = new SpannableString("设置背景颜色"); ss1.setSpan(new BackgroundColorSpan(Color.parseColor("#FFD700")), 0, ss1.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE); tv1.setText(ss1);
/** * Non-0-length spans of type SPAN_INCLUSIVE_EXCLUSIVE expand * to include text inserted at their starting point but not at their * ending point. When 0-length, they behave like marks. */ public static final int SPAN_INCLUSIVE_EXCLUSIVE = SPAN_MARK_MARK; /** * Spans of type SPAN_INCLUSIVE_INCLUSIVE expand * to include text inserted at either their starting or ending point. */ public static final int SPAN_INCLUSIVE_INCLUSIVE = SPAN_MARK_POINT; /** * Spans of type SPAN_EXCLUSIVE_EXCLUSIVE do not expand * to include text inserted at either their starting or ending point. * They can never have a length of 0 and are automatically removed * from the buffer if all the text they cover is removed. */ public static final int SPAN_EXCLUSIVE_EXCLUSIVE = SPAN_POINT_MARK; /** * Non-0-length spans of type SPAN_EXCLUSIVE_INCLUSIVE expand * to include text inserted at their ending point but not at their * starting point. When 0-length, they behave like points. */ public static final int SPAN_EXCLUSIVE_INCLUSIVE = SPAN_POINT_POINT;
tv1.append("1234");
tv2 = (TextView) this.findViewById(R.id.tv2); SpannableString ss2 = new SpannableString("点我吧123456"); ss2.setSpan(new ClickableSpan() { @Override public void onClick(View widget) { Log.i("lenve", "tv2---onClick"); Toast.makeText(MainActivity.this, "点我呀", Toast.LENGTH_SHORT) .show(); } }, 0, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); tv2.setText(ss2); // 设置tv2为可点击状态 tv2.setMovementMethod(LinkMovementMethod.getInstance());
tv3 = (TextView) this.findViewById(R.id.tv3); SpannableString ss3 = new SpannableString("设置文本颜色"); ss3.setSpan(new ForegroundColorSpan(Color.parseColor("#FF3030")), 0, ss3.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); tv3.setText(ss3);
tv6 = (TextView) this.findViewById(R.id.tv6); SpannableString ss6 = new SpannableString("删除线"); ss6.setSpan(new StrikethroughSpan(), 0, ss6.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); tv6.setText(ss6);
tv7 = (TextView) this.findViewById(R.id.tv7); SpannableString ss7 = new SpannableString("下划线"); ss7.setSpan(new UnderlineSpan(), 0, ss7.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); tv7.setText(ss7);
tv8 = (TextView) this.findViewById(R.id.tv8); SpannableString ss8 = new SpannableString("设置图片"); ss8.setSpan( // DynamicDrawableSpan.ALIGN_BASELINE表示依照基线对齐 // DynamicDrawableSpan.ALIGN_BOTTOM表示依照底部对齐 new DynamicDrawableSpan(DynamicDrawableSpan.ALIGN_BOTTOM) { @Override public Drawable getDrawable() { Drawable d = getResources().getDrawable( R.drawable.ic_launcher); d.setBounds(0, 0, 150, 150); return d; } }, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);// 这里的参数0,1表示将“设”字替换为图片 tv8.setText(ss8);
tv9 = (TextView) this.findViewById(R.id.tv9); SpannableString ss9 = new SpannableString("基于X轴缩放"); // ScaleXSpan中的参数大于1表示横向扩大,小于1大于0表示缩小,等于1表示正常显示 ss9.setSpan(new ScaleXSpan(2), 0, ss9.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); tv9.setText(ss9);只需要在setSpan方法中传入一个ScaleXSpan对象即可,里边参数含义看注释。
tv10 = (TextView) this.findViewById(R.id.tv10); SpannableString ss10 = new SpannableString("字体样式,粗体、斜体等"); ss10.setSpan(new StyleSpan(Typeface.BOLD), 5, 7, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); tv10.setText(ss10);
tv11 = (TextView) this.findViewById(R.id.tv11); SpannableString ss11 = new SpannableString("(x1 + x2)2 = x12+x22+2x1x2"); // 设置下标 ss11.setSpan(new SubscriptSpan(), 2, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); // 设置下标字体大小 ss11.setSpan(new AbsoluteSizeSpan(30), 2, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); ss11.setSpan(new SubscriptSpan(), 7, 8, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); ss11.setSpan(new AbsoluteSizeSpan(30), 7, 8, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); ss11.setSpan(new SubscriptSpan(), 14, 15, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); ss11.setSpan(new AbsoluteSizeSpan(30), 14, 15, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); ss11.setSpan(new SubscriptSpan(), 18, 19, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); ss11.setSpan(new AbsoluteSizeSpan(30), 18, 19, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); ss11.setSpan(new SubscriptSpan(), 23, 24, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); ss11.setSpan(new AbsoluteSizeSpan(30), 23, 24, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); ss11.setSpan(new SubscriptSpan(), 25, 26, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); ss11.setSpan(new AbsoluteSizeSpan(30), 25, 26, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); // 设置上标 ss11.setSpan(new SuperscriptSpan(), 9, 10, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); ss11.setSpan(new AbsoluteSizeSpan(30), 9, 10, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); ss11.setSpan(new SuperscriptSpan(), 15, 16, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); ss11.setSpan(new AbsoluteSizeSpan(30), 15, 16, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); ss11.setSpan(new SuperscriptSpan(), 19, 20, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); ss11.setSpan(new AbsoluteSizeSpan(30), 19, 20, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); tv11.setText(ss11);
tv13 = (TextView) this.findViewById(R.id.tv13); SpannableString ss13 = new SpannableString("打电话,发短信,发邮件,打开网页"); ss13.setSpan(new URLSpan("tel:13534884482"), 0, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); ss13.setSpan(new URLSpan("smsto:13534884482"), 4, 7, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); ss13.setSpan(new URLSpan("mailto:[email protected]"), 8, 11, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); ss13.setSpan(new URLSpan("http://www.baidu.com"), 12, 16, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); tv13.setText(ss13); tv13.setMovementMethod(LinkMovementMethod.getInstance());