博客
关于我
Android TextView 添加下划线的几种方式
阅读量:447 次
发布时间:2019-03-06

本文共 1928 字,大约阅读时间需要 6 分钟。

小编推荐
  
 
总结起来大概有5种做法: 

1. 将要处理的文字写到一个资源文件,如string.xml(使用html用法格式化)
 
2. 当文字中出现URL、E-mail、电话号码等的时候,可以将TextView的android:autoLink属性设置为相应的的值,如果是所有的类型都出来就是android:autoLink="all"当然也可以在java代码里 做,textView01.setAutoLinkMask(Linkify.ALL); 
 
3. 用
Html类的fromHtml()方法格式化要放到TextView里的文字 ,与第1种一样,只是是用代码动态设置
 
4. 设置TextView的Paint属性:tvTest.getPaint().setFlags(Paint. UNDERLINE_TEXT_FLAG ); //下划线

5. 用Spannable或实现它的类,如SpannableString来格式部分字符串。
   另外附上一篇博客介绍:
 
 
如果是在资源文件里:
1、字符串资源中设置下划线属性
phone:0123456
MyLink
直接让TextView引用字符串资源的name即可。 2、TextView设置autoLink属性

 

如果是代码里: 1、使用Html.fromHtml()
TextView textView = (TextView)findViewById(R.id.tv_test); textView.setText(Html.fromHtml(""+"0123456"+""));
2、使用TextView的Paint的属性
tvTest.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线tvTest.getPaint().setAntiAlias(true);//抗锯齿
3、使用SpannableString类
SpannableString content = new SpannableString(str);content.setSpan(new UnderLineSpan, 0, str.length(), 0);
代码里面自定义超链接样式:
TextView tv=new TextView(this);tv.setText(Html.fromHtml("自定义的超链接样式"));// 在单击链接时凡是有要执行的动作,都必须设置MovementMethod对象tv.setMovementMethod(LinkMovementMethod.getInstance());  CharSequence text  =  tv.getText();if (text instanceof Spannable){      int  end  =  text.length();        Spannable sp  =  (Spannable)tv.getText();        URLSpan[] urls = sp.getSpans( 0 , end, URLSpan.class );                     SpannableStringBuilder style = new  SpannableStringBuilder(text);        style.clearSpans(); // should clear old spans         for (URLSpan url : urls){               URLSpan myURLSpan=   new  URLSpan(url.getURL());                                  style.setSpan(myURLSpan,sp.getSpanStart(url),sp.getSpanEnd(url),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);            style.setSpan(new ForegroundColorSpan(0xFFFF0000), start, end, Spannable.SPAN_INCLUSIVE_INCLUSIVE);//设置前景色为红色
  }     tv.setText(style);  }
另外一篇文章中有几个具体的实例可以参考: 看完的顺手点歌赞呗,谢谢鼓励!

你可能感兴趣的文章
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named 'pandads'
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>
No qualifying bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs<?>‘ available
查看>>
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>
no session found for current thread
查看>>
No static resource favicon.ico.
查看>>
no such file or directory AndroidManifest.xml
查看>>
No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
查看>>
NO.23 ZenTaoPHP目录结构
查看>>
no1
查看>>