【问题标题】:add diagonal strike through text添加对角线删除文本
【发布时间】:2017-04-11 22:51:46
【问题描述】:

我正在开发一个数学应用程序,我需要用对角线删除文本,如果我使用删除线文本,则该行显示为水平,我不能使用 draw-able,因为我已经将它用于文本视图背景 我什至尝试在带有边框的可绘制文件中创建一条对角线,但运气不好我做不到。有没有办法做到这一点? 我正在附加我的文本视图背景文件

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

【问题讨论】:

    标签: android layout textview


    【解决方案1】:

    您必须创建 custom 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"/>
    

    【讨论】:

    • 感谢 nakul,但我需要在单击按钮时敲击文本,所以我如何以编程方式调用 obliqueStrike 文本视图。点击之前的文字应该是正常的..请帮帮我..
    • 一个简单的选项是隐藏/显示正常并点击TextView,点击按钮。
    • 你好 nakul,我正在尝试 ObliqueStrike 文本视图,但不幸的是我无法显示它我显示了我的部分代码..你能找出错误。
    • 你能提供你的邮件ID吗?
    • 你可以随时用相关代码提出新问题... :) 你不需要我 :)
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      在 Kotlin 中

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

      【讨论】:

        【解决方案4】:

        这是上面@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)
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-05-28
          • 2018-01-31
          • 1970-01-01
          • 2018-09-27
          • 2020-12-30
          • 1970-01-01
          • 2011-06-15
          • 1970-01-01
          相关资源
          最近更新 更多