您可以通过三种方式执行此操作,通过在TextView 中设置前景或在strings.xml 中设置PaintFlag 或将字符串声明为<strike>your_string</strike>。例如,
通过 PaintFlag
这是您只需在 TextView 上设置删除线标志的最简单方法,
yourTextView.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG);
它会穿透你的 TextView。
通过前景可绘制(仅适用于 API 23+)
如果您的 minSdkVersion 是 API 版本 23 +,那么您可以通过将前景设置为来遍历您的 TextView,
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false">
<shape android:shape="line">
<stroke android:width="1dp" android:color="@android:color/holo_red_dark"/>
</shape>
</item>
</selector>
现在,您只需在 TextView 中将上述可绘制对象设置为 foreground。例如,
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your Textview with StrikeThrough"
android:foreground="@drawable/strikethrough_foreground" /> <!-- this is available above --!>
通过strings.xml
在此方法中,您必须将strings.xml 中的字符串声明为删除线,
<string name="strike_line"> <strike>This line is strike throughed</strike></string>
注意
但我建议您通过设置前景可绘制来穿透您的 TextView。因为通过drawable,您可以轻松设置删除线颜色(就像我在上面的示例中设置为红色一样)或大小或任何其他样式属性。而在其他两种方法中,默认文本颜色是删除线颜色。