最近业务涉及到一个添加下划线的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"/>