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);
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: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);
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());
}
}
相关推荐
通过Spannable设置TextView文字样式,相关博文:http://www.cnblogs.com/tianzhijiexian/p/4222393.html
TextView中的超链接可以通过几种方式实现:1.Html.fromHtml,2.Spannable,3.Linkify.addLinks。下面分别进行测试,包括修改字体样式,下划线样式,点击事件等,需要的朋友可以参考下
android开发中使用Spannable设置超链接的方法
spannable textview是一个自定义的文本视图,允许您通过spannables自定义文本片段或语句的样式,但不必直接处理spannable本身。
android通过设置文字超链接启动activity
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 ...
这里介绍两种方法,一种是Spannable,一种是Html.fromHtml...String textSource = 修改TextView中部分文字的<font color='#ff0000'><big>大</big><small>小</small></font>和颜色,展示多彩效果!; textView.setText
设置TextView中文本的样式(如:颜色、斜体等),可以针对不同位置的文本设置不同的样式(如:将索引范围在1-3的字符设置为黑色,2-6的字符设置为粗体等) myFirstTextView = (TextView)findViewById(R.id....
特征 超链接点击事件(带有按下的文本和背景颜色) 单击事件(带有按下的文本和背景颜色) 文字背景色 文字前景色 字体大小 文本样式(粗体、斜体) 文字字体 图像当然,您可以使用 api addTypeSpan添加自定义字体...
主要介绍了Android TextView中文字通过SpannableString设置属性用法,结合实例形式分析了TextView控件中SpannableString类相关属性的使用技巧,需要的朋友可以参考下
TextView中设置后无效果。 android:shadowColor指定文本阴影的颜色,需要与shadowRadius一起使用。效果: android:shadowDx设置阴影横向坐标开始位置。 android:shadowDy设置阴影纵向坐标开始位置。 ...
PS:关于SpannableString相关的其他span,查看[Android教程]TextView使用SpannableString设置复合文本 2、使用Html.ImageGetter显示网页中的图片 查看文章:[Android教程]TextView显示Html类解析的网页和图片及自定义...
废话不多说了,直接给大家贴代码了,具体代码如下所示: ...import android.text.Spannable; import android.text.style.AbsoluteSizeSpan; import android.text.style.BackgroundColorSpan; import andro
Android Spannable in kotlin > Kotlin wrappers around SpannableStringBuilder. Inspired by binaryfork's Spanny. SpannableStringBuilder API is attractive, it's a nice way to styling text without split ...
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 ...
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 中使用 Spannable 可以实现 TextView 富文本的显示,但是在自定义控件中如何使用 Spannable 绘制不同样式的文字呢? 例如这种效果,标题中的 分数字61 是粗体,分 是常规字体,并且相对于 61 更...
安卓中的TextView 实现部分文字点击事件,通过Spannable 实现
使用Spannable设置TextView的字体的各种样式。详情请看http://blog.csdn.net/nxiangbo/article/details/51132737
默认状态下,同一个TextView显示的文本格式一致,运用Html.fromHtml( )方法或者SpannableString类可以设定不同的显示格式。参考: 编辑文章 - 博客频道 - CSDN.NET http://write.blog.csdn.net/postedit/40657573