Android TextView中文字通过SpannableString来设置超链接、颜色、字体等属性

来源:互联网 发布:淘宝店铺增加粉丝数量 编辑:程序博客网 时间:2024/04/17 07:15
在Android中,TextView是我们最常用的用来显示文本的控件。

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


   res-layout-main.xml

view plainprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_height="wrap_content" android:layout_width="wrap_content" android:orientation="horizontal">  
  4. <TextView    
  5.     android:id="@+id/myTextView"  
  6.     android:layout_width="fill_parent"   
  7.     android:layout_height="wrap_content"   
  8.     />  
  9. </LinearLayout>  


res-color-color.xml

res-color-linkcolor.xml

view plainprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector  xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <item android:state_pressed="true"  
  4.           android:color="#ffffff00"/> <!-- pressed -->  
  5.     <item android:state_focused="true"  
  6.           android:color="#ff00ffff"/> <!-- focused -->  
  7.     <item android:color="#ff0ff000"/> <!-- default -->  
  8. </selector>  


TextViewLinkActivity

view plainprint?
  1. package com.snowdream;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import org.xmlpull.v1.XmlPullParserException;  
  6.   
  7. import android.app.Activity;  
  8. import android.content.res.ColorStateList;  
  9. import android.content.res.XmlResourceParser;  
  10. import android.graphics.Color;  
  11. import android.os.Bundle;  
  12. import android.text.SpannableString;  
  13. import android.text.Spanned;  
  14. import android.text.method.LinkMovementMethod;  
  15. import android.text.style.AbsoluteSizeSpan;  
  16. import android.text.style.BackgroundColorSpan;  
  17. import android.text.style.BulletSpan;  
  18. import android.text.style.ForegroundColorSpan;  
  19. import android.text.style.RelativeSizeSpan;  
  20. import android.text.style.ScaleXSpan;  
  21. import android.text.style.StrikethroughSpan;  
  22. import android.text.style.StyleSpan;  
  23. import android.text.style.SubscriptSpan;  
  24. import android.text.style.SuperscriptSpan;  
  25. import android.text.style.TextAppearanceSpan;  
  26. import android.text.style.TypefaceSpan;  
  27. import android.text.style.URLSpan;  
  28. import android.text.style.UnderlineSpan;  
  29. import android.widget.TextView;  
  30.   
  31. public class TextViewLinkActivity extends Activity {  
  32.     TextView mTextView = null;     
  33.     SpannableString msp = null;    
  34.       
  35.     /** Called when the activity is first created. */  
  36.     @Override  
  37.     public void onCreate(Bundle savedInstanceState) {  
  38.         super.onCreate(savedInstanceState);  
  39.         setContentView(R.layout.main);  
  40.           
  41.         mTextView = (TextView)findViewById(R.id.myTextView);  
  42.           
  43.         //创建一个 SpannableString对象    
  44.         msp = new SpannableString("字体测试字体大小一半两倍前景色背景色正常粗体斜体粗斜体下划线删除线x1x2电话邮件网站短信彩信地图X轴综合");   
  45.           
  46.         //设置字体(default,default-bold,monospace,serif,sans-serif)  
  47.         msp.setSpan(new TypefaceSpan("monospace"), 0, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  
  48.         msp.setSpan(new TypefaceSpan("serif"), 2, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  
  49.           
  50.         //设置字体大小(绝对值,单位:像素)   
  51.         msp.setSpan(new AbsoluteSizeSpan(20), 4, 6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  
  52.         msp.setSpan(new AbsoluteSizeSpan(20,true), 6, 8, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  //第二个参数boolean dip,如果为true,表示前面的字体大小单位为dip,否则为像素,同上。  
  53.           
  54.         //设置字体大小(相对值,单位:像素) 参数表示为默认字体大小的多少倍  
  55.         msp.setSpan(new RelativeSizeSpan(0.5f), 8, 10, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  //0.5f表示默认字体大小的一半  
  56.         msp.setSpan(new RelativeSizeSpan(2.0f), 10, 12, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  //2.0f表示默认字体大小的两倍  
  57.           
  58.         //设置字体前景色  
  59.         msp.setSpan(new ForegroundColorSpan(Color.MAGENTA), 12, 15, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  //设置前景色为洋红色  
  60.           
  61.         //设置字体背景色  
  62.         msp.setSpan(new BackgroundColorSpan(Color.CYAN), 15, 18, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  //设置背景色为青色  
  63.        
  64.         //设置字体样式正常,粗体,斜体,粗斜体  
  65.         msp.setSpan(new StyleSpan(android.graphics.Typeface.NORMAL), 18, 20, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  //正常  
  66.         msp.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 20, 22, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  //粗体  
  67.         msp.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC), 22, 24, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  //斜体  
  68.         msp.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC), 24, 27, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  //粗斜体  
  69.           
  70.         //设置下划线  
  71.         msp.setSpan(new UnderlineSpan(), 27, 30, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  
  72.           
  73.         //设置删除线  
  74.         msp.setSpan(new StrikethroughSpan(), 30, 33, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  
  75.           
  76.         //设置上下标  
  77.         msp.setSpan(new SubscriptSpan(), 34, 35, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);     //下标     
  78.         msp.setSpan(new SuperscriptSpan(), 36, 37, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);   //上标              
  79.           
  80.         //超级链接(需要添加setMovementMethod方法附加响应)  
  81.         msp.setSpan(new URLSpan("tel:4155551212"), 37, 39, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);     //电话     
  82.         msp.setSpan(new URLSpan("mailto:webmaster@google.com"), 39, 41, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);     //邮件     
  83.         msp.setSpan(new URLSpan("http://www.baidu.com"), 41, 43, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);     //网络     
  84.         msp.setSpan(new URLSpan("sms:4155551212"), 43, 45, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);     //短信   使用sms:或者smsto:  
  85.         msp.setSpan(new URLSpan("mms:4155551212"), 45, 47, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);     //彩信   使用mms:或者mmsto:  
  86.         msp.setSpan(new URLSpan("geo:38.899533,-77.036476"), 47, 49, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);     //地图     
  87.           
  88.         //设置字体大小(相对值,单位:像素) 参数表示为默认字体宽度的多少倍  
  89.         msp.setSpan(new ScaleXSpan(2.0f), 49, 51, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //2.0f表示默认字体宽度的两倍,即X轴方向放大为默认字体的两倍,而高度不变  
  90.           
  91.         //设置字体(依次包括字体名称,字体大小,字体样式,字体颜色,链接颜色)  
  92.         ColorStateList csllink = null;  
  93.         ColorStateList csl = null;  
  94.         XmlResourceParser xppcolor=getResources().getXml (R.color.color);  
  95.         try {  
  96.             csl= ColorStateList.createFromXml(getResources(),xppcolor);  
  97.         }catch(XmlPullParserException e){  
  98.             // TODO: handle exception  
  99.             e.printStackTrace();          
  100.         }catch(IOException e){  
  101.             // TODO: handle exception  
  102.             e.printStackTrace();          
  103.         }  
  104.   
  105.         XmlResourceParser xpplinkcolor=getResources().getXml(R.color.linkcolor);  
  106.         try {  
  107.             csllink= ColorStateList.createFromXml(getResources(),xpplinkcolor);  
  108.         }catch(XmlPullParserException e){  
  109.             // TODO: handle exception  
  110.             e.printStackTrace();          
  111.         }catch(IOException e){  
  112.             // TODO: handle exception  
  113.             e.printStackTrace();          
  114.         }  
  115.         msp.setSpan(new TextAppearanceSpan("monospace",android.graphics.Typeface.BOLD_ITALIC, 30, csl, csllink), 51, 53, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);   
  116.        
  117.         //设置项目符号  
  118.         msp.setSpan(new BulletSpan(android.text.style.BulletSpan.STANDARD_GAP_WIDTH,Color.GREEN), 0 ,53, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //第一个参数表示项目符号占用的宽度,第二个参数为项目符号的颜色  
  119.   
  120.           
  121.         mTextView.setText(msp);  
  122.         mTextView.setMovementMethod(LinkMovementMethod.getInstance());   
  123.     }  

————————————————————————————

TextView是用来显示文本的,有时需要给TextView中的个别字设置为超链接,或者设置个别字的颜色、字体等,那就需要用到Spannable对象,可以借助Spannable对象实现以上设置

myTextView = (TextView) this.findViewById(R.id.myTextView);   
    
  //创建一个 SpannableString对象   
  SpannableString sp = new SpannableString("这句话中有百度超链接,有高亮显示,这样,或者这样,还有斜体.");   
  //设置超链接   
  sp.setSpan(new URLSpan("http://www.baidu.com"), 5, 7,   
  Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);   
  //设置高亮样式一   
  sp.setSpan(new BackgroundColorSpan(Color.RED), 17 ,19,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);   
  //设置高亮样式二   
  sp.setSpan(new ForegroundColorSpan(Color.YELLOW),20,24,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);   
  //设置斜体   
  sp.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC), 27, 29, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);   
  //SpannableString对象设置给TextView   
  myTextView.setText(sp);   
  //设置TextView可点击   
  myTextView.setMovementMethod(LinkMovementMethod.getInstance()); 

 

textview显示不同的颜色、样式来表示,需要用到SpannableString对象来处理

下面是从网上找的一段代码,已验证过:

mTextView = (TextView)findViewById(R.id.test);
SpannableString tSS = new SpannableString(“SpannableString学习中”);
tSS.setSpan(new BackgroundColorSpan(Color.RED), 0, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  //红色高亮
tSS.setSpan(new UnderlineSpan(), 15, 18,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);    //下划线
tSS.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC), 2, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //斜体
mTextView.setText(tSS);

关键方法:
public void setSpan (Object what, int start, int end, int flags)
主要是start跟end
start是起始位置,无论中英文,都算一个。从0开始计算起。end是结束位置,所以处理的文字,包含开始位置,但不包含结束位置。



下面解决点击不同文字跳转到不同activity的问题:

直接贴代码:

package com.feel;

import android.app.Activity;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class TextViewJump2Activity extends Activity {
    /** Called when the activity is first created. */
 private TextView mClickableText;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  LinearLayout layout = new LinearLayout(this);
  mClickableText = new TextView(this);
  mClickableText.setClickable(true);
  mClickableText.setTextSize(20);
 
  layout.addView(mClickableText);
  setContentView(layout);
  mClickableText.setText(getClickableSpan());
  mClickableText.setMovementMethod(LinkMovementMethod.getInstance());
 }

 private SpannableString getClickableSpan() {
  View.OnClickListener l = new View.OnClickListener() {
   //如下定义自己的动作
   public void onClick(View v) {
    Toast.makeText(TextViewJump2Activity.this, "Click Success", Toast.LENGTH_SHORT).show();

    //在这里就可以做跳转到activity或者弹出对话框的操作了
   }
  };

  SpannableString spanableInfo = new SpannableString("This is a test, Click Here!");
  int start = 16;
  int end = spanableInfo.length();
  spanableInfo.setSpan(new Clickable(l), start, end,
  Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
  return spanableInfo;
 }
}

class Clickable extends ClickableSpan implements OnClickListener {
 private final View.OnClickListener mListener;

 public Clickable(View.OnClickListener l) {
 mListener = l;
 }

 @Override
 public void onClick(View v) {
  mListener.onClick(v);
 }
}

0 0
原创粉丝点击