android 给TextView添加下划线

这篇博客介绍了如何在Android布局XML文件中为TextView设置下划线效果,通过示例代码展示了如何创建并应用下划线样式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.example.textviewbottomline;


import android.app.Activity;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ForegroundColorSpan;
import android.text.style.URLSpan;
import android.text.style.UnderlineSpan;
import android.text.util.Linkify;
import android.widget.TextView;


public class MainActivity extends Activity {
private TextView text_view0, text_view1, text_view2, text_view3, text_view4;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text_view0 = (TextView) findViewById(R.id.text_view0);
text_view1 = (TextView) findViewById(R.id.text_view1);
text_view2 = (TextView) findViewById(R.id.text_view2);
text_view3 = (TextView) findViewById(R.id.text_view3);
text_view4 = (TextView) findViewById(R.id.text_view4);


text_view0.setAutoLinkMask(Linkify.ALL);


text_view1.setText(Html.fromHtml("<u>李易峰</u>"));


text_view2.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
// 设置抗锯齿
text_view2.getPaint().setAntiAlias(true);


String str = "赵丽颖";
SpannableString spannableString = new SpannableString(str);
spannableString.setSpan(new UnderlineSpan(), 0, str.length(), 0);
text_view3.setText(spannableString);


text_view4.setText(Html.fromHtml("<a href=\"http://www.baidu.com\">百度</a>"));
// 在单击链接时凡是有要执行的动作,都必须设置MovementMethod对象
text_view4.setMovementMethod(LinkMovementMethod.getInstance());
CharSequence text = text_view4.getText();
if (text instanceof Spannable) {
int end = text.length();
Spannable sp = (Spannable) text_view4.getText();
URLSpan[] urls = sp.getSpans(0, end, URLSpan.class);
SpannableStringBuilder style = new SpannableStringBuilder(text);
style.clearSpans();
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(Color.MAGENTA), 0, text.length(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); // 设置前景色为洋红色
}
text_view4.setText(style);
}
}


}




<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        android:textColor="#3366cc"
        android:textSize="30sp" />


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:autoLink="all"
        android:text="http://www.baidu.com"
        android:textColor="#3366cc"
        android:textSize="30sp" />


    <TextView
        android:id="@+id/text_view0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:autoLink="all"
        android:text="http://www.baidu.com"
        android:textColor="#3366cc"
        android:textSize="30sp" />


    <TextView
        android:id="@+id/text_view1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="李易峰"
        android:textColor="#3366cc"
        android:textSize="30sp" />


    <TextView
        android:id="@+id/text_view2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="杨紫"
        android:textColor="#3366cc"
        android:textSize="30sp" />


    <TextView
        android:id="@+id/text_view3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="赵丽颖"
        android:textColor="#3366cc"
        android:textSize="30sp" />


    <TextView
        android:id="@+id/text_view4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="唐艺昕"
        android:textColor="#3366cc"
        android:textSize="30sp" />


</LinearLayout>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值