public class HighLightActivity extends Activity { String strs="我的心太乱了,给我点空白。"; TextView textview; int start =3; int end = 5; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.high_light); textview=(TextView)findViewById(R.id.textview); SpannableStringBuilder style=new SpannableStringBuilder(strs); style.setSpan(new BackgroundColorSpan(Color.RED),start,end,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); textview.setText(style); } }
同时设置文字和背景高亮显示:
Java代码
public class HighLightActivity extends Activity { String strs="我的心太乱了,给我点空白。"; TextView textview; int start =3; int end = 5; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.high_light); textview=(TextView)findViewById(R.id.textview); SpannableStringBuilder style=new SpannableStringBuilder(strs); style.setSpan(new BackgroundColorSpan(Color.RED),start,end,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); style.setSpan(new ForegroundColorSpan(Color.RED),7,9,Spannable.SPAN_EXCLUSIVE_INCLUSIVE); textview.setText(style); } }
参数说明:
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
API里面解释:Spans of type SPAN_EXCLUSIVE_EXCLUSIVE do not expand to include text inserted at either their starting or ending point. They can never have a length of 0 and are automatically removed from the buffer if all the text they cover is removed.
即在原文本头或尾追加新文本的样式不受原文本样式影响,原文本高亮,新追加文本不高亮。
Spannable.SPAN_EXCLUSIVE_INCLUSIVE
API里面解释:Non-0-length spans of type SPAN_INCLUSIVE_EXCLUSIVE expand to include text inserted at their ending point but not at their starting point. When 0-length, they behave like points.
即在原文本尾追加新文本的样式受原文本样式影响,原来文本尾高亮,新追加文本也高亮
例子2:
功能:实现在一个TextView里显示了一行字符串,需要这个串里字符显示不同颜色.
代码如下:
attrs.xml文件:
第二种方式:
String newMessageInfo = "" + 红色内容
+ "TextView学习显示不同颜色";
mTextView.setTextView(Html.fromHtml(newMessageInfo));
例子3:
SpannableString ss = new SpannableString( "红色打电话粗体删除线绿色下划线图片:.");
ss.setSpan(new ForegroundColorSpan(Color.RED), 0, 2,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new URLSpan("tel:4155551212"), 2, 5,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new StyleSpan(Typeface.BOLD), 5, 7, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new StrikethroughSpan(), 7, 10,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new UnderlineSpan(), 10, 16,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new ForegroundColorSpan(Color.GREEN), 10, 15, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
Drawable d = getResources().getDrawable(R.drawable.icon48x48_1);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
ss.setSpan(span, 18, 19, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
TextView t4 = (TextView) findViewById(R.id.text4);
t4.setText(ss);
t4.setMovementMethod(LinkMovementMethod.getInstance());
例子4:
TextView是用来显示文本的,有时需要给TextView中的个别字设置为超链接,或者设置个别字的颜色、字体等,那就需要用到Spannable对象,可以借助Spannable对象实现以上设置。
效果图:
Activity代码:
public class TextViewLinkActivity extends Activity { TextView myTextView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); 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()); } }
昨晚研读 ApiDemo 源码至 com.example.android.apis.text.Link 类。首先,看一下其运行效果:
要给 TextView 加上效果,方式主要有几种:
第一种,自动应用效果,使用 android:autolink 属性,如:
- "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"
- />
第二种,在文本中使用 标签,如:
第三种,和第二种其实是一样的,只不过将文本改在 JAVA 代码中,如:
- TextView t3 = (TextView) findViewById(R.id.text3);
- t3.setText(
- Html.fromHtml(
- "text3: Text with a " +
- "link " +
- "created in the Java source code using HTML."));
- t3.setMovementMethod(LinkMovementMethod.getInstance());
第四种,前面三种可以说都是自动的,而第四种就是纯“手工”的了。通过创建 SpanableString 字符串,并在之上创 建一个或多个 Span 来实现丰富的效果。例子如下:
- 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:
- "1.0" encoding="utf-8"?>
- "http://schemas.android.com/apk/res/android"
- android:layout_height="wrap_content" android:layout_width="wrap_content" android:orientation="horizontal">
-
- android:id="@+id/myTextView"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
-
res-color-color.xml
res-color-linkcolor.xml:
- "1.0" encoding="utf-8"?>
- "http://schemas.android.com/apk/res/android">
-
- "true"
- android:color="#ffffff00"/>
-
- "true"
- android:color="#ff00ffff"/>
-
- "#ff0ff000"
/>
-
TextViewLinkActivity:
- 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;
-
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- mTextView = (TextView)findViewById(R.id.myTextView);
-
-
- msp = new SpannableString("字体测试字体大小一半两倍前景色背景色正常粗体斜体粗斜体下划线删除线x1x2电话邮件网站短信彩信地图X轴综合/bot");
-
-
- 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);
-
-
- msp.setSpan(new RelativeSizeSpan(0.5f), 8, 10, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
- msp.setSpan(new RelativeSizeSpan(2.0f), 10, 12, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
-
-
- 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);
-
-
- msp.setSpan(new URLSpan("tel:4155551212"), 37, 39, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
- msp.setSpan(new URLSpan("mailto:[email protected]"), 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);
- msp.setSpan(new URLSpan("mms:4155551212"), 45, 47, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
- 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);
-
-
- ColorStateList csllink = null;
- ColorStateList csl = null;
- XmlResourceParser xppcolor=getResources().getXml (R.color.color);
- try {
- csl= ColorStateList.createFromXml(getResources(),xppcolor);
- }catch(XmlPullParserException e){
-
- e.printStackTrace();
- }catch(IOException e){
-
- e.printStackTrace();
- }
-
- XmlResourceParser xpplinkcolor=getResources().getXml(R.color.linkcolor);
- try {
- csllink= ColorStateList.createFromXml(getResources(),xpplinkcolor);
- }catch(XmlPullParserException e){
-
- e.printStackTrace();
- }catch(IOException e){
-
- 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());
- }
- }
效果预览: