版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq77485042/article/details/79475998
最近公司在做一个商城类应用,使用过商城类型App的都知道,有时候一些产品在搞活动时,会有特价情况,这样的话会把原价放在优惠价的下面,在原价的字体中画一根横线。要实现这样的效果非常简单:
public class DrawLineTextView extends TextView {
public DrawLineTextView(Context context) {
this(context, null);
}
public DrawLineTextView(Context context, AttributeSet attrs) {
super(context, attrs);
getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
}
}
写一个类继承子原生的TextView,只需要在构造方法中设置:
getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
拿到该textview的画笔,设置标志就行了。然后就像用普通的textview的方式使用就行了。
xml中的代码:
<com.stinfo.app.main.widget.DrawLineTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="¥498.00"
android:textColor="#99282525"
android:textSize="@dimen/x28" />
效果图如下: