Android 通过文本添加斜线

Android 通过文本添加斜线,android,layout,textview,Android,Layout,Textview,我正在开发一个数学应用程序,我需要用斜线划出文本,如果我使用横线划出文本,我就不能使用draw-Abable,因为我已经在使用它作为文本视图背景 我甚至试着在有边框的可绘制文件中创建一条对角线,但运气不佳,我做不到。有没有办法做到这一点? 我正在附加文本视图背景文件 <?xml version="1.0" encoding="utf-8" ?> <shape xmlns:android="http://schemas.android.com/apk/res/andr

我正在开发一个数学应用程序,我需要用斜线划出文本,如果我使用横线划出文本,我就不能使用draw-Abable,因为我已经在使用它作为文本视图背景 我甚至试着在有边框的可绘制文件中创建一条对角线,但运气不佳,我做不到。有没有办法做到这一点? 我正在附加文本视图背景文件

    <?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>
您必须创建自定义的TextView来实现这一点

这将帮助您创建它

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);
    }
}
通过使用包完全限定视图,可以在布局文件中使用它,如下所示:

<your.package.name.ObliqueStrikeTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1234567890"
        android:textSize="20sp"/>

我只想在@Nakul的答案中添加一个小小的修改,即不需要为穿透的长度定义静态维度,您可以得到textview本身的宽度

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);
    }
}
在科特林

public class ObliqueStrikeTextView : TextView {


private var dividerColor: Int = 0
private lateinit var paint: Paint

constructor(context: Context) : super(context) {
    init(context)
}

constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
    init(context)
}

constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle) {
    init(context)
}

private fun init(context: Context) {
    val resources = context.resources
    //replace with your color
    dividerColor = resources.getColor(R.color.black)

    paint = Paint()
    paint.apply {
        color = dividerColor
        //replace with your desired width
        strokeWidth = resources.getDimension(width)
    }

}

override fun onDraw(canvas: Canvas) {
    super.onDraw(canvas)
    if(::paint.isInitialized){
        canvas.drawLine(0.0f, height.toFloat(), width.toFloat(), 0.0f, paint)
    }
}
}

这是上面@sunil答案的精简版本。 此外,我还更改了删除线的角度,使其在文本视图上看起来更好

class ObliqueStrikeTextView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyle: Int = 0)
    : TextView(context, attrs, defStyle) {

    private var dividerColor: Int = 0
    private var paint: Paint

    init {
        dividerColor = ContextCompat.getColor(context, R.color.redCarnation)
        paint = Paint().apply {
            color = dividerColor
            strokeWidth = resources.getDimension(R.dimen.strikethrough_line_width)
        }
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        //reduce angle by 20%
        val startY = height * 0.8f
        val stopY = height - startY
        canvas.drawLine(0.0f, startY, width.toFloat(), stopY, paint)
    }
}

谢谢nakul,但是我需要在单击按钮时敲打文本,所以我如何通过编程调用obliqueStrike文本视图。点击之前,文本应该是正常的。请帮我解决问题。一个简单的选项是:点击按钮,隐藏/显示正常和删除文本视图。hello nakul,我正在尝试AbliqueStrike文本视图,但不幸的是,我无法显示它,我显示了我的部分代码..你能找出错误吗?你能给出你的邮件id吗?你可以随时用相关代码问一个新问题…:你不需要我来帮你: