Android TextView设置下划线

来源:互联网 发布:软件测试优秀方案 编辑:程序博客网 时间:2024/05/18 00:45

最近总是遇到Textview加下划线,所以抽空总结一下

Android TextView加下划线的方法大概有五种:

一、代码设置TextView的Paint属性

首先声明控件并初始化,然后设置属性
//TextView加下划线TextView tv1 = (TextView)findViewById(R.id.text);tv1.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);//下划线tv1.getPaint().setAntiAlias(true);//抗锯齿tv1.setTextColor(Color.parseColor("#ff0000"));

二、使用html用法

首先将文字写在资源文件 strings.xml里

<resources>    <string name="app_name">textview</string>    <string name="phone"><u>那就这样吧</u></string></resources>

然后在布局文件里使用。

<TextView    android:id="@+id/text2"    android:layout_width="wrap_content"    android:layout_marginTop="20dp"    android:layout_height="wrap_content"    android:layout_centerInParent="true"    android:layout_gravity="center_horizontal"    android:text="@string/phone"    android:textSize="20sp" />

这样下划线就添加成功了。最近喜欢听那就这样吧 这首歌.........

三、用Html类的fromHtml()方法。

这种方法和第二种类似,区别是这种方法是用代码设置

//代码使用Html.fromHtml()TextView tv4 = (TextView)findViewById(R.id.text4);tv4.setText(Html.fromHtml("<u>"+"没有什么能够阻挡,我对自由的向往"+"</u>"));
这样就设置成功了。

四、TextView的android:autoLink属性

比如一段话中有URL,电话,邮件等的时候,可以设置该属性。例如 我的邮箱是:liying5494@163.com.然后设置TextView的android:autoLink属性,就会自动在邮箱下添加下划线。文字下是没有的。属性有多种可以根据需求选择。

<TextView    xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/text3"    android:layout_width="wrap_content"    android:layout_marginTop="20dp"    android:layout_height="wrap_content"    android:layout_centerInParent="true"    android:layout_gravity="center_horizontal"    android:autoLink="all"    android:text="邮箱:liying5494@163.com 电话:10000"    android:textSize="20sp" />

五、Spannable或实现它的类,如SpannableString来格式部分字符串。

SpannableString的功能有很多,比如添加背景色,文字颜色,修饰效果,添加下划线等等。
我是通过下面这篇博客学习的:

http://blog.csdn.net/u011102153/article/details/52485650



0 0
原创粉丝点击