有2中方法
- //中画线
- textView.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG); // 设置中划线并加清晰
- //下划线
- textView.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
- //取消设置的线
- textView.getPaint().setFlags(0);
- //抗锯齿
- textView.getPaint().setAntiAlias(true);
自定义下划线UnderlineTextView
public class UnderlineTextView extends android.support.v7.widget.AppCompatTextView {
//Paint即画笔,在绘图过程中起到了极其重要的作用,画笔主要保存了颜色,
//样式等绘制信息,指定了如何绘制文本和图形,画笔对象有很多设置方法,
//大体上可以分为两类,一类与图形绘制相关,一类与文本绘制相关
private final Paint paint = new Paint();
//下划线高度
private int underlineHeight = 0;
//下划线颜色
private int underLineColor;
//通过new创建实例是调用这个构造函数
//这种情况下需要添加额外的一些函数供外部来控制属性,如set*(...);
public UnderlineTextView(Context context) {
this(context, null);
}
//通过XML配置但不定义style时会调用这个函数
public UnderlineTextView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
//获取自定义属性
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.UnderlineTextView);
//获取具体属性值
underLineColor = typedArray.getColor(R.styleable.UnderlineTextView_underline_color, getTextColors().getDefaultColor());
underlineHeight = (int) typedArray.getDimension(R.styleable.UnderlineTextView_underline_height,
TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, getResources().getDisplayMetrics()));
}
//通过XML配置且定义样式时会调用这个函数
public UnderlineTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
//防止下划线高度大到一定值时会覆盖掉文字,需从写此方法
@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);
//设置下划线颜色
paint.setColor(underLineColor);
//float left, float top, float right, float bottom
canvas.drawRect(0, getHeight() - underlineHeight, getWidth(), getHeight(), paint);
}
}
style 里面
<!-- UnderlineTextView -->
<declare-styleable name="UnderlineTextView">
<attr name="underline_color" format="color"/>
<attr name="underline_height" format="dimension"/>
</declare-styleable>
xml使用
声明命名空间 xmlns:app="http://schemas.android.com/apk/res-auto"
<xxx.UnderlineTextView
android:id="@+id/tv_forget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/colorPrimary"
app:underline_color="@color/colorPrimary"
app:underline_height="1dp" />
效果图

本文介绍了一种在Android应用中自定义TextView的方法,用于实现文本下方带有可定制颜色及高度的下划线效果。通过继承AppCompatTextView并利用Paint对象绘制下划线,同时提供了XML配置选项以调整下划线的颜色和高度。

2504

被折叠的 条评论
为什么被折叠?



