通过文字添加对角线打击

时间:2016-11-28 09:06:39

标签: android layout textview

我正在开发一个数学应用程序,我需要用对角线打击文本,如果我使用穿透文本,该行显示为水平,我不能使用drawable,因为我已经将它用于文本视图背景 我甚至尝试在带有边框的可绘制文件中创建对角线,但没有运气我无法做到。反正有没有实现这个目标? 我正在附加我的文本视图背景文件

    <?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>

4 个答案:

答案 0 :(得分:1)

您必须创建自定义 TextView才能实现此目标。

answer将帮助您创建它。

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

答案 1 :(得分:0)

我只想在@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);
    }
}

答案 2 :(得分:0)

在科特林

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)
    }
}
}

答案 3 :(得分:0)

这是上面@sunil的答案的精简版。 另外,我更改了删除线的角度,以使其在TextViews上看起来更好一些。

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)
    }
}