通过文字添加对角线 [英] add diagonal strike through text
问题描述
我正在开发一个数学应用程序,其中我需要用对角线来绘制文本,如果我使用删除线文本,则该行显示为水平,我不能使用可绘制,因为我已经在文本视图中使用它了背景 我什至尝试在带有边框的可绘制文件中创建一条对角线,但是不走运,我做不到.反正有实现这个目标的方法吗? 我要附加我的文本视图背景文件
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>
推荐答案
您将必须创建自定义 TextView
来实现此目的.
You will have to create custom TextView
which can achieve this.
此答案将帮助您创建它.
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"/>
这篇关于通过文字添加对角线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!