TextView设置超链接
第一种方式:autoLink属性
默认是none.
在Java代码中设置setAutoLinkMask(int);
参数和上边列表(除了none之外一一对应)对应,分别为:Linkify.ALL,Linkify.EMAIL_ADDRESSES,Linkify.MAP_ADDRESSES,Linkify.PHONE_NUMBERS,Linkify.WEB_URLS
第二种方式:FromXml
例如:
<string name="link_text_auto"><b>text_auto_linkify: Various kinds
of data that will be auto-linked.</b>
In this text are some things that are actionable. For instance,
you can click on http://www.google.com and it will launch the
web browser. You can click on google.com too. If you
click on (415) 555-1212 it should dial the phone. Or just write
foobar@example.com for an e-mail link. If you have a URI like
http://www.example.com/lala/foobar@example.com you should get the
full link not the e-mail address. Or you can put a location
like 1600 Amphitheatre Parkway, Mountain View, CA 94043. To summarize:
https://www.google.com, or 650-253-0000, somebody@example.com,
or 9606 North MoPac Expressway, Suite 400, Austin, TX 78759.</string>
textView.setText(Html.fromHtml(getString(R.string.link_text_auto)));
第三种方式:SpannableString
SpannableString ss = new SpannableString( "text_spannable: Manually created spans. Click here to dial the phone.");
ss.setSpan(new StyleSpan(Typeface.BOLD), 0, 39, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
ss.setSpan(new URLSpan("tel:4155551212"), 40 + 6, 40 + 10, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
TextView设置阴影
android:shadowColor //指定文本阴影的颜色
android:shadowDx //设置阴影横向坐标开始位置
android:shadowDy //设置阴影纵向坐标开始位置
android:shadowRadius //设置阴影的半径。设置为0.1会变成字体的颜色
代码中通过方法public void setShadowLayer (float radius, float dx, float dy, int color)来设置。
TextView设置行间距
1、android:lineSpacingExtra
设置行间距,如”5dp”。
2、android:lineSpacingMultiplier
设置行间距的倍数,如”1.2″。
TextView去掉默认padding
includefontpadding=false
字符串资源替换
%d (表示整数)
%f (表示浮点数)
%s (表示字符串)
<string name="test">呵呵%1$s,哈哈%2$s</string>
getString(R.string.test,"66666","7777")
TextView设置多种字体大小颜色等
String text = "哈哈哈哈哈哈哈哈1234啊哈哈哈";
int start = text.indexOf('5');
int end = text.length();
Spannable textSpan = new SpannableStringBuilder(text);
textSpan.setSpan(new AbsoluteSizeSpan(16), 0, start, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
textSpan.setSpan(new AbsoluteSizeSpan(26), start, end - 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
textSpan.setSpan(new AbsoluteSizeSpan(16), end - 1, end, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
TextView textView = (TextView) findViewById(R.id.text);
textView.setText(textSpan);
上图是一些span