如何在TextView上斜线删除 [英] How to strike through obliquely on TextView
问题描述
有没有简单的方法可以在TextView上倾斜绘制?现在,我正在使用以下代码:
Is there any simple way how to draw obliquely strike through on TextView? Now I'm using this code:
textview.setPaintFlags(textview.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
但是我需要这样的东西:
我对Paint API不太熟悉,如何简单地做到这一点,所以将不胜感激.
谢谢.
But I need something like this:
I'm not very familiar with Paint API how to simply achive this so any help will be appreciated.
Thank you.
推荐答案
使用自定义的 TextView
类非常容易.有关文档的更多信息,请查看画布指南..请注意您可以在其中更改颜色/宽度的注释:
It's pretty easy with a custom TextView
class. Check out the Canvas guide from the documentation for more info on this. Note the comments where you can change the color/width:
public class ObliqueStrikeTextView extends TextView
{
private int dividerColor;
private Paint paint;
public ObliqueStrikeTextView(Context context)
{
super(context);
init(context);
}
public ObliqueStrikeTextView(Context context, AttributeSet attrs)
{
super(context, attrs);
init(context);
}
public ObliqueStrikeTextView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
init(context);
}
private void init(Context context)
{
Resources resources = context.getResources();
//replace with your color
dividerColor = resources.getColor(R.color.black);
paint = new Paint();
paint.setColor(dividerColor);
//replace with your desired width
paint.setStrokeWidth(resources.getDimension(R.dimen.vertical_divider_width));
}
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
canvas.drawLine(0, getHeight(), getWidth(), 0, paint);
}
}
您可以通过用包装完全限定视图的外观,从而在布局文件中使用它,如下所示:
You can use it in a layout file by fully qualifying the view with your package, like this:
<your.package.name.ObliqueStrikeTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1234567890"
android:textSize="20sp"/>
这是最终结果:
这篇关于如何在TextView上斜线删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!