码迷,mamicode.com
首页 > 移动开发 > 详细

Android#SpannableString

时间:2015-04-22 15:28:25      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:android   样式   textview   spannablestring   

SpannableString可以在TextView或者EditText中设置字体、大小、颜色、样式、上下标、删除线、下划线、超链接、点击事件等。


       用法如下:

//初始化SpannableString
SpannableString ss = new SpannableString("将要处理的文字");
//使用SpannableString的setSpan()方法,设置SpannableString要显示的形式
ss.setSpan (Object what, int start, int end, int flags);
//把SpannableString赋值给Textview
textView.setText(ss);



       其中,flags的含义略提一下:

Spanned.SPAN_EXCLUSIVE_EXCLUSIVE//前后插入的内容都不会跟着样式改变;
Spanned.SPAN_INCLUSIVE_EXCLUSIVE//前面插入的内容会跟着样式改变,后面不会;
Spanned.SPAN_EXCLUSIVE_INCLUSIVE//前面插入的内容不会跟着样式改变,后面会跟着改变;
Spanned.SPAN_INCLUSIVE_INCLUSIVE//前后插入的内容都会跟着样式改变;


       使用的时候,根据具体情况选择(TextView时都一样)


       很明显startend就是要设置的文字的开始结束的索引,注意这里的开始和和结束也是包含头不包含尾的;


        剩下的就是这个Object的what了,SpannableString要显示成什么样子,就看他了;

        

        

//设置字体
public TypefaceSpan(String family)//family为字体,如"monospace"、"serif"、"sans-serif"等

//设置字体大小
public AbsoluteSizeSpan(int size)//size单位为px
public AbsoluteSizeSpan(int size, boolean dip)//dip单位是否为dip,false为px
public RelativeSizeSpan(float proportion)//proportion为正常字号的倍数,如0.5f即正常字号的0.5倍
public ScaleXSpan(float proportion)//proportion为正常字号宽度的倍数 ,如3.0f即正常字号宽度的3倍

//设置字体颜色
public ForegroundColorSpan(int color)//color为颜色值,如Color.RED或Color.parseColor("#000000")等

//设置字体背景颜色
public BackgroundColorSpan(int color)//color为颜色值,如Color.RED或Color.parseColor("#000000")等

//设置字体样式
public StyleSpan(int style)
//style为具体样式的常量值:
//android.graphics.Typeface.BOLD//粗体
//android.graphics.Typeface.BOLD_ITALIC//粗体+斜体
//android.graphics.Typeface.ITALIC//斜体
//android.graphics.Typeface.NORMAL//正常
                                                                                         
//设置上标
public SuperscriptSpan() 
           
//设置下标
public SubscriptSpan()
       
//设置下划线
public UnderlineSpan()

//设置删除线
public StrikethroughSpan()
         
//设置超链接,需要textView.setMovementMethod(LinkMovementMethod.getInstance())才能生效
public URLSpan(String url)//url为URI,如"http://wjyzxc.blog.51cto.com/5725897/1637082 "、"tel:10010"、
"mailto:lfe99@vip.qq.com"或"sms:10010"等

//自定义点击事件
//继承ClickableSpan,在public void onClick(View v)方法中写点击事件
//实现点击事件的同时,还可以重写public void updateDrawState(TextPaint ds):                  @Override
    public void updateDrawState(TextPaint ds) {
        ds.setColor(Color.RED);//字体颜色
        ds.setUnderlineText(true);//是否有下划线
    }
    
//设置图片
public ImageSpan(Context context, Bitmap b)//还有Drawable等重载方法

//设置字体,样式,大小、颜色等
public TextAppearanceSpan(String family, int style, int size,ColorStateList color, ColorStateList linkColor)

//设置项目符号
public BulletSpan()

        例:

ss.setSpan(new UnderlineSpan(), 2, 5,  Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new  SuperscriptSpan(), 4, 5,  Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

本文出自 “許妳柒世溫柔” 博客,请务必保留此出处http://wjyzxc.blog.51cto.com/5725897/1637082

Android#SpannableString

标签:android   样式   textview   spannablestring   

原文地址:http://wjyzxc.blog.51cto.com/5725897/1637082

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!