`
hunankeda110
  • 浏览: 740438 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

android中的spannable的使用

阅读更多
昨晚研读 ApiDemo 源码至 com.example.android.apis.text.Link 类。首先,看一下其运行效果:

要给 TextView 加上效果,方式主要有几种:

第一种,自动应用效果,使用 android:autolink 属性,如:
Java代码 复制代码 收藏代码
  1. <TextView xmlns:android="http://schemas.android.com/apk/res/android"  
  2.             android:id="@+id/text1"  
  3.             android:layout_width="match_parent"  
  4.             android:layout_height="match_parent"  
  5.             android:autoLink="all"  
  6.             android:text="@string/link_text_auto"  
  7.             />  
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/text1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:autoLink="all"
            android:text="@string/link_text_auto"
            />


第二种,在文本中使用 <a> 标签,如:
Java代码 复制代码 收藏代码
  1. <string name="link_text_manual"><b>text2:</b> This is some other   
  2.       text, with a <a href="http://www.google.com">link</a> specified   
  3.       via an &lt;a&gt; tag.  Use a \"tel:\" URL   
  4.       to <a href="tel:4155551212">dial a phone number</a>   
  5. </string>  
<string name="link_text_manual"><b>text2:</b> This is some other
      text, with a <a href="http://www.google.com">link</a> specified
      via an &lt;a&gt; tag.  Use a \"tel:\" URL
      to <a href="tel:4155551212">dial a phone number</a>
</string>


第三种,和第二种其实是一样的,只不过将文本改在 JAVA 代码中,如:
Java代码 复制代码 收藏代码
  1. TextView t3 = (TextView) findViewById(R.id.text3);   
  2. t3.setText(   
  3.     Html.fromHtml(   
  4.          "<b>text3:</b>  Text with a " +   
  5.          "<a href=\"http://www.google.com\">link</a> " +   
  6.          "created in the Java source code using HTML."));   
  7. t3.setMovementMethod(LinkMovementMethod.getInstance());  
TextView t3 = (TextView) findViewById(R.id.text3);
t3.setText(
    Html.fromHtml(
         "<b>text3:</b>  Text with a " +
         "<a href=\"http://www.google.com\">link</a> " +
         "created in the Java source code using HTML."));
t3.setMovementMethod(LinkMovementMethod.getInstance());


第四种,前面三种可以说都是自动的,而第四种就是纯“手工”的了。通过创建 SpanableString 字符串,并在之上创 建一个或多个 Span 来实现丰富的效果。例子如下:
Java代码 复制代码 收藏代码
  1. SpannableString ss = new SpannableString("text4: Click here to dial the phone.");   
  2. ss.setSpan(new StyleSpan(Typeface.BOLD), 06,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);   
  3. ss.setSpan(new URLSpan("tel:4155551212"), 1317,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);   
  4.   
  5. TextView t4 = (TextView) findViewById(R.id.text4);   
  6. t4.setText(ss);   
  7. t4.setMovementMethod(LinkMovementMethod.getInstance());  
SpannableString ss = new SpannableString("text4: Click here to dial the phone.");
ss.setSpan(new StyleSpan(Typeface.BOLD), 0, 6,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new URLSpan("tel:4155551212"), 13, 17,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

TextView t4 = (TextView) findViewById(R.id.text4);
t4.setText(ss);
t4.setMovementMethod(LinkMovementMethod.getInstance());


完整的代码见 ApiDemo 吧,下面我提几点需要注意的:

.setMovementMethod,此方法在需要响应用户事件时使用,如点击一个电话号码就跳转到拨号页面。如果不执行这个方法是不会响应事件的,即便文本看着已经是下划线蓝色字了。
.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE,这是在 setSpan 时需要指定的 flag,它的意义我试了很久也没试出来,睡个觉,今天早上才突然有点想法,试之,果然。它是用来标识在 Span 范围内的文本前后输入新的字符时是否把它们也应用这个效果。分别有 Spanned.SPAN_EXCLUSIVE_EXCLUSIVE(前后都不包括)、Spanned.SPAN_INCLUSIVE_EXCLUSIVE(前面包括,后面不包括)、Spanned.SPAN_EXCLUSIVE_INCLUSIVE(前面不包括,后面包括)、Spanned.SPAN_INCLUSIVE_INCLUSIVE(前后都包括)。看个截图就更明白了:


对比一下




以下转自:http://blog.csdn.net/yang_hui1986527/article/details/6776629
在Android中,TextView是我们最常用的用来显示文本的控件。

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

res-layout-main.xml:
Java代码 复制代码 收藏代码
  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>  
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content" android:layout_width="wrap_content" android:orientation="horizontal">
<TextView  
	android:id="@+id/myTextView"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    />
</LinearLayout>


res-color-color.xml

res-color-linkcolor.xml:

Java代码 复制代码 收藏代码
  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>  
<?xml version="1.0" encoding="utf-8"?>
<selector  xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:color="#ffffff00"/> <!-- pressed -->
    <item android:state_focused="true"
          android:color="#ff00ffff"/> <!-- focused -->
    <item android:color="#ff0ff000"/> <!-- default -->
</selector>


TextViewLinkActivity:

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


效果预览:

分享到:
评论
3 楼 AndroidLond 2016-05-06  
引用
引用
引用
[u][u][u][u][u][u][b][/b][/u][/u][/u][/u][/u][/u]
      
2 楼 mengxiangqipa 2015-03-03  
       
1 楼 mkrcpp 2014-12-10  
赞一个,厉害

相关推荐

    Android代码-kotlin实现的Android Spannable API

    Android Spannable in kotlin &gt; Kotlin wrappers around SpannableStringBuilder. Inspired by binaryfork's Spanny. SpannableStringBuilder API is attractive, it's a nice way to styling text without split ...

    Android代码-A simple spannable string helper

    This libary aims to simplify the creation of spannable string. Features [x] long click event [x] bind an object with the clicked text [x] click event (with pressed color state) [x] text background ...

    Android 自定义 View 中使用 Spannable的实例详解

    我们都知道 Android 中使用 Spannable 可以实现 TextView 富文本的显示,但是在自定义控件中如何使用 Spannable 绘制不同样式的文字呢? 例如这种效果,标题中的 分数字61 是粗体,分 是常规字体,并且相对于 61 更...

    Spannable设置超链接

    android开发中使用Spannable设置超链接的方法

    Android TextView中文字通过SpannableString设置属性用法示例

    主要介绍了Android TextView中文字通过SpannableString设置属性用法,结合实例形式分析了TextView控件中SpannableString类相关属性的使用技巧,需要的朋友可以参考下

    Android开发EditText属性.txt

    此属性在EditView中使用,但是这里也可以用。 android:imeOptions附加功能,设置右下角IME动作与编辑框相关的动作,如actionDone右下角将显示一个“完成”,而不设置默认是一个回车符号。这个在EditView中再详细...

    Android代码-Android HTML to TextView Builder 辅助工具库。

    There is a lovely method on the android.text.Html class, fromHtml(), that converts HTML into a Spannable for use with a TextView. However, the documentation does not stipulate what ...

    Android中SpannableString实例程序

    这是一个通过Spannable来丰富TextView显示效果的demo程序。

    Android代码-dante

    Dante is a text parser to easily generate a Spannable from a raw input, right now it supports only HTML but the idea is to be able to support multiple input types (e.g., MarkDown). Originally I ...

    Android-SpannableBar是一个Grid风格的spannablebar

    SpannableBar是一个Grid风格的spannable bar,当你需要一种跨栏的方式时,这非常有用。 该视图允许您设置起始栏,跨度,栏数等。

    Android代码-Emoticons-Keyboard

    spannable strings for showing emoticons Usage This project is not a library. It is just a common android application in which i have just try to implement a keyboard (actually a dialog box) for ...

    HtmlDsl:为 Android TextView 构建有效的 HTML

    android.text.Html类中有一个可爱的方法fromHtml ,可以将 HTML 转换为Spannable以与TextView一起使用。 然而,文档并没有规定支持哪些 HTML 标签,这使得这种方法有点碰运气。 这个小型库提供了一个 DSL,用于为...

    SimplifySpan:易于使用且功能强大的Spannable库

    一个易于使用且功能强大的Spannable库 截屏 摇动 implementation 'com.github.iwgang:simplifyspan:2.2' 支持单位 SpecialTextUnit 文本(构造函数|字符串) 重力(setGravity(gravity | int))SpecialGravity....

    android SpannableString + 图文混排 + @功能

    最近项目里因为有一些图文混排的需求,需要在Android TextView 增加多种类型的数据,诸如图片、按钮、拨号、链接、@功能等等。 示例1:使用 SpannableString,试了一下效果一般。(新手推荐) 示例2:后面也是参考了...

    android通过设置文字超链接启动activity

    android通过设置文字超链接启动activity

    Android利用SpannableString实现格式化微博内容

    主要介绍了Android利用SpannableString实现格式化微博内容的相关资料,文中介绍的非常详细,对大家具有一定的参考借鉴价值,需要的朋友们下面来一起看看吧。

    Android代码-html-textview

    HtmlTextView is an extended TextView component for Android, which can load HTML and converts it into Spannable for displaying it. It is a replacement for usage of the WebView component, which behaves ...

    RichText:android textview的富文本

    富文本 该库将带有自定义标签的字符串解析为Spannable字符串。特征 超链接点击事件(带有按下的文本和背景颜色) 单击事件(带有按下的文本和背景颜色) 文字背景色 文字前景色 字体大小 文本样式(粗体、斜体) ...

    Android图文混排发帖

    今日需要实现一个发帖功能,找了些APP进行参考,最终选择知乎图文混排的形式发帖,用户体验感更好。 看了知乎源码才知道,知乎的发帖是用webview实现的,我们需要原生开发,得自己写一个。

Global site tag (gtag.js) - Google Analytics