您的位置:首页 > 其它

TextView SpannableString类属性详细解释

2015-08-18 10:22 267 查看
Android中若要一行文字有N种不同的效果,或者可以点击进行跳转页面或者是进行网页链接,那么SpannableString类是最好的选择,工作用到了这个知识点,进行总结一下,以下是SpannableString的多种属性:

1、BackgroundColorSpan 背景色

2、ClickableSpan 文本可点击,有点击事件

3、ForegroundColorSpan 文本颜色(前景色)

4、MaskFilterSpan 修饰效果,如模糊(BlurMaskFilter)、浮雕(EmbossMaskFilter)

5、MetricAffectingSpan 父类,一般不用

6、RasterizerSpan 光栅效果

7、StrikethroughSpan 删除线(中划线)

8、SuggestionSpan 相当于占位符

9、UnderlineSpan 下划线

10、AbsoluteSizeSpan 绝对大小(文本字体)

11、DynamicDrawableSpan 设置图片,基于文本基线或底部对齐。

12、ImageSpan 图片

13、RelativeSizeSpan 相对大小(文本字体)

14、ReplacementSpan 父类,一般不用

15、ScaleXSpan 基于x轴缩放

16、StyleSpan 字体样式:粗体、斜体等

17、SubscriptSpan 下标(数学公式会用到)

18、SuperscriptSpan 上标(数学公式会用到)

19、TextAppearanceSpan 文本外貌(包括字体、大小、样式和颜色)

20、TypefaceSpan 文本字体

21、URLSpan 文本超链接

package
com.example.aaa;

import android.app.Activity;

import android.graphics.Color;

import android.os.Bundle;

import android.text.SpannableString;

import android.text.TextPaint;

import android.text.method.LinkMovementMethod;

import android.text.style.AbsoluteSizeSpan;

import android.text.style.BackgroundColorSpan;

import android.text.style.ClickableSpan;

import android.text.style.ForegroundColorSpan;

import android.text.style.StrikethroughSpan;

import android.text.style.URLSpan;

import android.text.style.UnderlineSpan;

import android.view.View;

import android.widget.TextView;

import android.widget.Toast;

public class MainActivity extends Activity {

private TextView text_one;

private TextView text_two;

private TextView text_three;

private TextView text_four;

private TextView text_five;

private TextView text_six;

private TextView text_senver;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

text_one = (TextView) findViewById(R.id.text_one);

text_two = (TextView) findViewById(R.id.text_two);

text_three = (TextView) findViewById(R.id.text_three);

text_four = (TextView) findViewById(R.id.text_four);

text_five = (TextView) findViewById(R.id.text_five);

text_six = (TextView) findViewById(R.id.text_six);

text_senver = (TextView) findViewById(R.id.text_senver);

SpannableString span = new SpannableString("这是清甘茶的博客");

SpannableString span2 = new SpannableString("这是清甘茶的博客");

SpannableString span3 = new SpannableString("这是清甘茶的博客");

SpannableString span4 = new SpannableString("这是清甘茶的博客");

SpannableString span5 = new SpannableString("这是清甘茶的博客");

SpannableString span6 = new SpannableString("点击进入清甘茶的博客");

SpannableString span7 = new SpannableString("textView的点击事件");

// new BackgroundColorSpan为背景颜色

// new ForegroundColorSpan为字体的颜色

// new StrikethroughSpan为删除线(中划线)

// new UnderlineSpan为下划线

// new AbsoluteSizeSpan绝对字体的大小

span.setSpan(

new BackgroundColorSpan(Color.argb(0xFF, 0X3c, 0X62, 0X9D)), 0,

span.length(), SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);

span2.setSpan(

new ForegroundColorSpan(Color.argb(0xFF, 0X3c, 0X62, 0X9D)), 0,

span.length(), SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);

span3.setSpan(new StrikethroughSpan(), 0, 5,

SpannableString.SPAN_EXCLUSIVE_INCLUSIVE);

span4.setSpan(new UnderlineSpan(), 0, span4.length() - 2,

SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);

span5.setSpan(new AbsoluteSizeSpan(30, true), 0, 5,

SpannableString.SPAN_EXCLUSIVE_INCLUSIVE);

span6.setSpan(new URLSpan("http://my.csdn.net/my/mycsdn"), 0,

span6.length(), SpannableString.SPAN_EXCLUSIVE_INCLUSIVE);

NoLineClickSpan clickSpan = new NoLineClickSpan(text_senver.getText()

.toString());

span7.setSpan(clickSpan, 0, span7.length(),

SpannableString.SPAN_INCLUSIVE_INCLUSIVE);

text_two.append(span2);

text_three.append(span3);

text_four.append(span4);

text_five.append(span5);

text_six.append(span6);

// 设置让url可以点击

text_six.setMovementMethod(new LinkMovementMethod());

text_senver.append(span7);

text_senver.setMovementMethod(LinkMovementMethod.getInstance());

}

private class NoLineClickSpan extends ClickableSpan {

String text;

public NoLineClickSpan(String text) {

super();

this.text = text;

}

@Override

public void updateDrawState(TextPaint ds) {

super.updateDrawState(ds);

ds.setColor(ds.linkColor);

ds.setUnderlineText(false);// 取消下划线

}

@Override

public void onClick(View widget) {

//点击触发点击事件

Toast.makeText(MainActivity.this, "您点击了文字,点击事件触发",

Toast.LENGTH_SHORT).show();

}

}

}

下图是实验的效果图片,其中有七个例子,,,对应每个编号



下面是代码的链接:http://download.csdn.net/detail/qinggancha/9018885
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: