实现带有下划线的TextView

本文介绍了在Android中为TextView添加下划线的五种方法:通过drawable自定义XML,使用SpannableString动态设置,利用Paint.setFlags(),在string.xml设置属性,以及使用android:autoLink属性。此外,还提到了自定义带下划线的TextView的实现步骤,包括在attrs.xml定义属性和创建UnderLineTextView类。

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

最近业务涉及到一个添加下划线的TextView,所以在这里记录一下实现的几种方法。

第一种:在drawable中自定义一个xml文件,可以自定义横线位置、高度和颜色。

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

<!--gravity 可以设置线的位置如: bottom下划线,center中划线
      height:设置线的宽度  -->
    <item android:gravity="bottom"
        android:height="1dp">
        <shape>
            <!--下划线颜色-->
            <solid android:color="@color/colorAccent"/>
        </shape>

    </item>
</layer-list>

使用:直接设置为TextView的background即可:

 <TextView
        android:id="@+id/tv_one"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@id/checkbox"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:background="@drawable/bg_textview"
        android:text="第一种方法:自定义drawable布局设置background"/>

第二种:代码里动态设置

 1,在通过SpannableString在代码里动态设置:

 mTvTwo = (TextView) findViewById(R.id.tv_two);
        String text = mTvTwo.getText().toString().trim();
        SpannableString content = new SpannableString(text);
        content.setSpan(new UnderlineSpan(), 0, text.length(), 0);
        mTvTwo.setText(content);

  另外附上一篇博客介绍:Android TextView中文字通过SpannableString来设置超链接、颜色、字体等属性

https://blog.youkuaiyun.com/snowdream86/article/details/6776629

2,通过Paint.setFlags()属性在代码里动态设置

 mTextView = (TextView) findViewById(R.id.tv_underlint_text);

 mTextView.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);

第三种:字符串资源文件string.xml里面设置下划线属性

    <string name="text_info"><u>第三种方式:在string.xml文件里通过设置下划线属性</u></string>

使用:直接引用

 <TextView
        android:id="@+id/tv_three"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@id/tv_two"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:text="@string/text_info"/>

第四种:使用android:autoLink 属性设置

 当文字中出现URL、E-mail、电话号码等的时候,可以将TextView的android:autoLink属性设置为相应的的值,如果是所有的类型都出来就是android:autoLink="all",当然也可以在java代码里 做,textView01.setAutoLinkMask(Linkify.ALL); 

 当文字中出现URL、E-mail、电话号码等的时候,可以将TextView的android:autoLink属性设置为相应的的值,
如果是所有的类型都出来就是android:autoLink="all",
当然也可以在java代码里 做,textView01.setAutoLinkMask(Linkify.ALL); 

 <TextView
        android:id="@+id/tv2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@id/tv_three"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:autoLink="all"
        android:text="第四种方法: 设置autoLink属性:https://www.baidu.com/ "/>

第五种方法:自定义带下划线的TextView

首先需要在values资源目录下的attrs.xml(没有请新建)中定义好我们要自定义的属性:

    <declare-styleable name="underlineTextView">
        <attr name="underline_color" format="color"/>
        <attr name="underline_height" format="dimension"/>
        <attr name="underline_width" format="dimension"/>
    </declare-styleable>

然后写UnderLineTextView这个类:

public class UnderlineTextView extends android.support.v7.widget.AppCompatTextView{

    //画笔:
    private Paint mPaint = new Paint();
    //下划线缩进宽度
    private int underlineWidth = 0;
    //下划线高度
    private int underlineHeight = 0;
    //下划线颜色
    private int underlineColor;

    public UnderlineTextView(Context context) {
        this(context, null);
    }

    public UnderlineTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public UnderlineTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.underlineTextView);
        underlineColor = array.getColor(R.styleable.underlineTextView_underline_color, getTextColors().getDefaultColor());
        underlineHeight = array.getDimensionPixelSize(R.styleable.underlineTextView_underline_height, 2);
        underlineWidth =  array.getDimensionPixelSize(R.styleable.underlineTextView_underline_width,10);

        array.recycle();
    }

    //防止下划线高度大到一定值时会覆盖掉文字,需从写此方法
    @Override
    public void setPadding(int left, int top, int right, int bottom) {
        super.setPadding(left, top, right, bottom + underlineHeight);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //设置下划线颜色
        mPaint.setColor(underlineColor);

        canvas.drawRect(0 + underlineWidth,getHeight() - underlineHeight, getWidth() - underlineWidth, getHeight(), mPaint);
    }

使用:

 <com.demo.UnderlineTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@id/tv_four"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        app:underline_color="@color/colorAccent"
        app:underline_height="1dp"
        android:text="自定义带下划线的TextView"/>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值