create different color strike through

后端 未结 3 468
死守一世寂寞
死守一世寂寞 2020-12-20 02:27

I am trying to create textview with text color as black and strikthrough as red, I tried to use html but does not seems to work

String styledText = \"

        
相关标签:
3条回答
  • 2020-12-20 02:33
    @Override
    protected void onDraw(Canvas canvas) {
    
        super.onDraw(canvas);
        drawStrikeThroughPaint(canvas);
    }
    
    public void drawStrikeThroughPaint(Canvas canvas) {
        canvas.drawLine(0, getMeasuredHeight() / 2, getMeasuredWidth(), getMeasuredHeight() / 2, strikethroughPaint);
    }
    
    0 讨论(0)
  • 2020-12-20 02:42

    late to the party but use a layer-list and place it on top of the textview background. works perfectly. Let me show you.

    create in the drawables folder a file called strikethru.xml:

     <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item>
            <shape android:shape="rectangle">
                <solid android:color="#FFFFFF"/>
            </shape>
        </item>
        <item>
            <shape android:shape="line">
                <stroke android:width="1dp"
                    android:color="#8d8d8d"/> <!-- adjust color you want here -->
            </shape>
        </item>
    </layer-list>
    

    then in your textview do this:

        <TextView
                                    android:id="@+id/tv_toolbar_prod_price"
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
    
                                    android:paddingRight="3dp"
                                    android:text="1,290 B"      
    
    android:background="@drawable/strikethru_line"
                                    android:textColor="#070707"
                                    android:textSize="13sp" />
    

    the padding right of 3dp makes the strike through come out more from the text to give a real world effect.

    0 讨论(0)
  • 2020-12-20 02:52

    If you want to strike through all the text in the TextView you can simply create a sub-class of TextView in which you draw the strike line with the color of your choice.

    0 讨论(0)
提交回复
热议问题