TextView添加下划线有4种方式
<1>把想要处理的文字写在一个资源文件里,(String.xml)(使用html语法格式化)就可实现下划线的功能
<resources>
<string name="app_name">Dissertation</string>
<!--在strings.xml中进行语法格式化,实现html中的超链接效果<u>.....</u>-->
<string name="hello_blank_fragment">Hello blank fragment</string>
<string name="title"><u>Hello Word!</u></string>
<string name="content"><u>你好</u></string>
</resources>
<!--给TextView字体设置颜色,下划线颜色也改变-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/title"
android:textColor="@color/colorHtml"/>
<!--在colors.xml中设置颜色-->
<color name="colorHtml">#153880</color>
<2>当文字出现URL,E-mail,电话号码等的时候,可以将TextView的android:autoLink属性设置为相应的值,如果是所有的类型出来就是android:autoLink=”all”,也可以在Java代码中设置tv.setAutoLinkMask(Linkify.ALL);
<!--设置TextView的autoLink属性-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="all"
android:text="www.baidu.com"/>
<3>用HTML类的fromHtml()方法格式化要放到TextView里的文字,与第一种相似,只是用代码动态设置
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="www.baidu.com"
android:textColor="@color/colorHtml"/>
Java代码:
private void initView(View view) {
tv = ((TextView) view.findViewById(R.id.tv));
tv.setText(Html.fromHtml("<u>www.baidu.com</u>"));
}
<4>设置TextView的Paint属性
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="www.baidu.com"
android:textColor="@color/colorHtml"/>
private void initView(View view) {
tv = ((TextView) view.findViewById(R.id.tv));
tv.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);//下划线
tv.getPaint().setAntiAlias(true);//抗锯齿
}