TextView常用功能

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值