TextView 加下划线 、 中划线

本文介绍了一种在Android应用中自定义TextView的方法,用于实现文本下方带有可定制颜色及高度的下划线效果。通过继承AppCompatTextView并利用Paint对象绘制下划线,同时提供了XML配置选项以调整下划线的颜色和高度。
有2中方法
  1. //中画线
  2. textView.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG); // 设置中划线并加清晰  
  3. //下划线
  4. textView.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); 
  5. //取消设置的线
  6. textView.getPaint().setFlags(0);
  7. //抗锯齿
  8. 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" />

效果图


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值