Android TextView 添加下划线的几种方式
来源:互联网 发布:php 上传工具 编辑:程序博客网 时间:2024/06/09 21:20
总结起来大概有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、字符串资源中设置下划线属性
<resources> <string name="hello"><u>phone:0123456</u></string> <string name="app_name">MyLink</string></resources>
直接让TextView引用字符串资源的name即可。
2、TextView设置autoLink属性
<TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/text1" android:layout_width="match_parent" android:layout_height="match_parent" android:autoLink="all" android:text="@string/link_text_auto" />
如果是代码里:
1、使用Html.fromHtml()
TextView textView = (TextView)findViewById(R.id.tv_test); textView.setText(Html.fromHtml("<u>"+"0123456"+"</u>"));
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("<a href=\"http://blog.csdn.net/CAIYUNFREEDOM\">自定义的超链接样式</a>"));// 在单击链接时凡是有要执行的动作,都必须设置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//设置前景色为红色 } tv.setText(style); }
0 0
- Android TextView 添加下划线的几种方式
- Android TextView 添加下划线的几种方式
- Android TextView 添加下划线的几种方式
- Android TextView 添加下划线的几种方式
- Android TextView 添加下划线的几种方式
- android里TextView加下划线的几种方式 以及 为TextView添加边框
- Android里TextView加下划线的几种方式
- android里TextView加下划线的几种方式
- android里TextView加下划线的几种方式
- android TextView 添加下划线
- [Android 小知识点] textview 设置下划线的两种方式
- android里TextView加下划线的三种方式
- TextView加下划线的3种方式
- TextView 加下划线的2种方式
- android TextView 添加一个下划线
- android 给TextView添加下划线
- Android Textview 添加下划线 方法
- TextView文字下划线的添加
- spring AOP详解之--前置增强 (MethodBeforeAdvice)
- 设计模式之单例模式
- 使用Spring的@Scheduled实现定时任务
- MyEclipse破解版+tomcat安装+jdk
- [Python 爬虫之路4] 使用selenium爬取知乎任意一个问题下,所有回答中的图片
- Android TextView 添加下划线的几种方式
- Spring Web MVC基础的一个小实例
- 详解google Chrome浏览器(理论篇)
- Android框架/系统服务是怎样管理第三方Search数据源的?
- 《趣学Python编程》笔记---第一部分:学习编程(2)
- Activiry的启动模式
- int、integer比较。。。。
- 关于面试跳槽之类的
- 牛客模拟一:连续整数