add diagonal strike through text

2019-09-07 21:43发布

问题:

I am developing an math app where i need to strike the text with diagonal line,if i use strike-through-text the line appears horizontal ,i can't use draw-able because I am already using it for text view background i tried even creating a diagonal line in drawable file with borders ,but no luck I can't do it. Is there anyway to achieve this? I am attaching my text view background file

    <?xml version="1.0" encoding="utf-8" ?>
  <shape xmlns:android="http://schemas.android.com/apk/res/android"
   android:thickness="0dp"
   android:shape="rectangle">
 <stroke android:width="3dp"
  android:color="#4799E8"/>
 <corners android:radius="5dp" />
  <gradient
    android:startColor="#ffffff"
   android:endColor="#FFFFFF"
   android:type="linear"
   android:angle="270"/> 
  </shape>

回答1:

You will have to create custom TextView which can achieve this.

This answer will help you create it.

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"/>


回答2:

I just want to add a slight modification in @Nakul's answer which is that you don't need to define static dimension for the length of strike through, you can get the width of textview itself.

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
          /*Modification*/
        //Instead of providing static width you can pass the width of textview itself like this
        paint.setStrokeWidth(this.getWidth());
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);
        canvas.drawLine(0, getHeight(), getWidth(), 0, paint);
    }
}