当前位置: 移动技术网 > IT编程>移动开发>Android > Android SpannableString设置超链接、颜色、字体等属性

Android SpannableString设置超链接、颜色、字体等属性

2019年07月24日  | 移动技术网IT编程  | 我要评论

沧缘亭,敌法师闪来闪去消你魔法,中国的军事武器

android spannablestring设置超链接、颜色、字体等属性

在android中,textview是我们最常用的用来显示文本的控件。

  一般情况下,textview中的文本都是一个样式。那么如何对于textview中各个部分的文本来设置字体,大小,颜色,样式,以及超级链接等属性呢?下面我们通过spannablestring的具体实例操作来演示一下。

package com.snowdream;  
  
import java.io.ioexception;  
  
import org.xmlpull.v1.xmlpullparserexception;  
  
import android.app.activity;  
import android.content.res.colorstatelist;  
import android.content.res.xmlresourceparser;  
import android.graphics.bitmap;  
import android.graphics.bitmapfactory;  
import android.graphics.color;  
import android.graphics.drawable.drawable;  
import android.os.bundle;  
import android.text.spannablestring;  
import android.text.spanned;  
import android.text.method.linkmovementmethod;  
import android.text.style.absolutesizespan;  
import android.text.style.backgroundcolorspan;  
import android.text.style.bulletspan;  
import android.text.style.drawablemarginspan;  
import android.text.style.foregroundcolorspan;  
import android.text.style.iconmarginspan;  
import android.text.style.imagespan;  
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.widget.textview;  
  
public class textviewlinkactivity extends activity {  
  textview mtextview = null;    
  spannablestring msp = null;   
    
  /** called when the activity is first created. */  
  @override  
  public void oncreate(bundle savedinstancestate) {  
    super.oncreate(savedinstancestate);  
    setcontentview(r.layout.main);  
      
    mtextview = (textview)findviewbyid(r.id.mytextview);  
      
    //创建一个 spannablestring对象   
    msp = new spannablestring("字体测试字体大小一半两倍前景色背景色正常粗体斜体粗斜体下划线删除线x1x2电话邮件网站短信彩信地图x轴综合/bot");   
      
    //设置字体(default,default-bold,monospace,serif,sans-serif)  
    msp.setspan(new typefacespan("monospace"), 0, 2, spanned.span_exclusive_exclusive);  
    msp.setspan(new typefacespan("serif"), 2, 4, spanned.span_exclusive_exclusive);  
      
    //设置字体大小(绝对值,单位:像素)   
    msp.setspan(new absolutesizespan(20), 4, 6, spanned.span_exclusive_exclusive);  
    msp.setspan(new absolutesizespan(20,true), 6, 8, spanned.span_exclusive_exclusive); //第二个参数boolean dip,如果为true,表示前面的字体大小单位为dip,否则为像素,同上。  
      
    //设置字体大小(相对值,单位:像素) 参数表示为默认字体大小的多少倍  
    msp.setspan(new relativesizespan(0.5f), 8, 10, spanned.span_exclusive_exclusive); //0.5f表示默认字体大小的一半  
    msp.setspan(new relativesizespan(2.0f), 10, 12, spanned.span_exclusive_exclusive); //2.0f表示默认字体大小的两倍  
      
    //设置字体前景色  
    msp.setspan(new foregroundcolorspan(color.magenta), 12, 15, spanned.span_exclusive_exclusive); //设置前景色为洋红色  
      
    //设置字体背景色  
    msp.setspan(new backgroundcolorspan(color.cyan), 15, 18, spanned.span_exclusive_exclusive); //设置背景色为青色  
     
    //设置字体样式正常,粗体,斜体,粗斜体  
    msp.setspan(new stylespan(android.graphics.typeface.normal), 18, 20, spanned.span_exclusive_exclusive); //正常  
    msp.setspan(new stylespan(android.graphics.typeface.bold), 20, 22, spanned.span_exclusive_exclusive); //粗体  
    msp.setspan(new stylespan(android.graphics.typeface.italic), 22, 24, spanned.span_exclusive_exclusive); //斜体  
    msp.setspan(new stylespan(android.graphics.typeface.bold_italic), 24, 27, spanned.span_exclusive_exclusive); //粗斜体  
      
    //设置下划线  
    msp.setspan(new underlinespan(), 27, 30, spanned.span_exclusive_exclusive);  
      
    //设置删除线  
    msp.setspan(new strikethroughspan(), 30, 33, spanned.span_exclusive_exclusive);  
      
    //设置上下标  
    msp.setspan(new subscriptspan(), 34, 35, spanned.span_exclusive_exclusive);   //下标    
    msp.setspan(new superscriptspan(), 36, 37, spanned.span_exclusive_exclusive);  //上标        
      
    //超级链接(需要添加setmovementmethod方法附加响应)  
    msp.setspan(new urlspan("tel:4155551212"), 37, 39, spanned.span_exclusive_exclusive);   //电话    
    msp.setspan(new urlspan("mailto:webmaster@google.com"), 39, 41, spanned.span_exclusive_exclusive);   //邮件    
    msp.setspan(new urlspan("http://www.baidu.com"), 41, 43, spanned.span_exclusive_exclusive);   //网络    
    msp.setspan(new urlspan("sms:4155551212"), 43, 45, spanned.span_exclusive_exclusive);   //短信  使用sms:或者smsto:  
    msp.setspan(new urlspan("mms:4155551212"), 45, 47, spanned.span_exclusive_exclusive);   //彩信  使用mms:或者mmsto:  
    msp.setspan(new urlspan("geo:38.899533,-77.036476"), 47, 49, spanned.span_exclusive_exclusive);   //地图    
      
    //设置字体大小(相对值,单位:像素) 参数表示为默认字体宽度的多少倍  
    msp.setspan(new scalexspan(2.0f), 49, 51, spanned.span_exclusive_exclusive); //2.0f表示默认字体宽度的两倍,即x轴方向放大为默认字体的两倍,而高度不变  
      
    //设置字体(依次包括字体名称,字体大小,字体样式,字体颜色,链接颜色)  
    colorstatelist csllink = null;  
    colorstatelist csl = null;  
    xmlresourceparser xppcolor=getresources().getxml (r.color.color);  
    try {  
      csl= colorstatelist.createfromxml(getresources(),xppcolor);  
    }catch(xmlpullparserexception e){  
      // todo: handle exception  
      e.printstacktrace();      
    }catch(ioexception e){  
      // todo: handle exception  
      e.printstacktrace();      
    }  
  
    xmlresourceparser xpplinkcolor=getresources().getxml(r.color.linkcolor);  
    try {  
      csllink= colorstatelist.createfromxml(getresources(),xpplinkcolor);  
    }catch(xmlpullparserexception e){  
      // todo: handle exception  
      e.printstacktrace();      
    }catch(ioexception e){  
      // todo: handle exception  
      e.printstacktrace();      
    }  
    msp.setspan(new textappearancespan("monospace",android.graphics.typeface.bold_italic, 30, csl, csllink), 51, 53, spanned.span_exclusive_exclusive);   
     
    //设置项目符号  
    msp.setspan(new bulletspan(android.text.style.bulletspan.standard_gap_width,color.green), 0 ,msp.length(), spanned.span_exclusive_exclusive); //第一个参数表示项目符号占用的宽度,第二个参数为项目符号的颜色  
  
    //设置图片  
    drawable drawable = getresources().getdrawable(r.drawable.icon);   
    drawable.setbounds(0, 0, drawable.getintrinsicwidth(), drawable.getintrinsicheight());   
    msp.setspan(new imagespan(drawable), 53, 57, spanned.span_exclusive_exclusive);  
      
    mtextview.settext(msp);  
    mtextview.setmovementmethod(linkmovementmethod.getinstance());   
  }  
}  


感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网