本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《
阿里云开发者社区用户服务协议
》和
《
阿里云开发者社区知识产权保护指引
》。如果您发现本社区中有涉嫌抄袭的内容,填写
侵权投诉表单
进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
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: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());
Android开发中,TextView类的控件应该说是很常用了。一般来说我们是通过android:textSize="20sp"
来设置字体大小,但是很多时候也需要动态设置字体大小,调用也很简单:
textView.
jarvanmo