creating a strikethrough text?

前端 未结 11 886
借酒劲吻你
借酒劲吻你 2020-11-30 17:36

Can I create a strikethrough text in Android, I mean adding a special value in the TextView tag that can make this possible?



        
相关标签:
11条回答
  • 2020-11-30 18:18

    This fits nicely into databinding:

    @BindingAdapter("strikethrough")
    @JvmStatic
    fun strikethrough(view: TextView, show: Boolean) {
        view.paintFlags = if (show) {
            view.paintFlags or STRIKE_THRU_TEXT_FLAG
        } else {
            view.paintFlags and Paint.STRIKE_THRU_TEXT_FLAG.inv()
        }
    }
    

    Then in your xml:

        <TextView
            android:id="@+id/line_item_name"
            android:textAppearance="?attr/textAppearanceBody2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Club sandwich with ranch dressing"
            app:strikethrough="@{viewModel.isItemChecked}"/>
    
    0 讨论(0)
  • 2020-11-30 18:20

    try this :

    richTextView = (TextView)findViewById(R.id.rich_text);  
    
        // this is the text we'll be operating on  
        SpannableString text = new SpannableString("Lorem ipsum dolor sit amet");  
    
        text.setSpan(new ForegroundColorSpan(Color.RED), 0, 5, 0);  
    
        // make "ipsum" (characters 6 to 11) one and a half time bigger than the textbox  
        text.setSpan(new RelativeSizeSpan(1.5f), 6, 11, 0);  
    
        // make "dolor" (characters 12 to 17) display a toast message when touched  
        final Context context = this;  
        ClickableSpan clickableSpan = new ClickableSpan() {  
            @Override  
            public void onClick(View view) {  
                Toast.makeText(context, "dolor", Toast.LENGTH_LONG).show();  
            }  
        };  
        text.setSpan(clickableSpan, 12, 17, 0);  
    
        // make "sit" (characters 18 to 21) struck through  
        text.setSpan(new StrikethroughSpan(), 18, 21, 0);  
    
        // make "amet" (characters 22 to 26) twice as big, green and a link to this site.  
        // it's important to set the color after the URLSpan or the standard  
        // link color will override it.  
        text.setSpan(new RelativeSizeSpan(2f), 22, 26, 0);  
        text.setSpan(new URLSpan("http://www.djsad.com"), 22, 26, 0);  
        text.setSpan(new ForegroundColorSpan(Color.GREEN), 22, 26, 0);  
    
        // make our ClickableSpans and URLSpans work  
        richTextView.setMovementMethod(LinkMovementMethod.getInstance());  
    
        // shove our styled text into the TextView          
        richTextView.setText(text, BufferType.SPANNABLE); 
    
    0 讨论(0)
  • 2020-11-30 18:20

    I am just copying my answer. Hope it will help for someone If you have a single word we can use drawable. Following is the example:

    <item android:state_pressed="false"><shape android:shape="line">
            <stroke android:width="2dp" android:color="#ffffff" />
        </shape>
    </item>
    

    if you have multiple lines you can use the following code:

    TextView someTextView = (TextView) findViewById(R.id.some_text_view);
    someTextView.setText(someString);
    someTextView.setPaintFlags(someTextView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG)
    
    0 讨论(0)
  • 2020-11-30 18:26

    Paint.STRIKE_THRU_TEXT_FLAG

    TextView someTextView = (TextView) findViewById(R.id.some_text_view);
    someTextView.setText(someString);
    someTextView.setPaintFlags(someTextView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
    

    For painting text, there are several bit flags for doing things like bold, italics, and yes strikethrough. So to enable the strikethrough, you need to flip the bit that corresponds to this flag. The easiest way to do this is to use a bitwise-or on the current flags and a constant that corresponds to a set of flags with only the strikethrough flag enabled.

    Edit from Comment by Ε Г И І И О :

    For any one wanting to remove this flag, this is how:

    someTextView.setPaintFlags(someTextView.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));
    
    0 讨论(0)
  • 2020-11-30 18:26

    I tried few options above but, this works best for me:

    String text = "<strike><font color=\'#757575\'>Some text</font></strike>";
    textview.setText(Html.fromHtml(text));
    

    cheers

    0 讨论(0)
  • 2020-11-30 18:28

    You can do this in three ways, by either setting foreground in TextView or setting PaintFlag or declaring a string as <strike>your_string</strike> in strings.xml. For example,

    Through PaintFlag

    This is the simplest method you just have to set strikethrough flag on your TextView as,

    yourTextView.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG);
    

    it will strike through your TextView.

    Through foreground drawable(Works only for API 23+)

    If your minSdkVersion is API version 23 +, then you can strike through your TextView by setting a foreground as,

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_pressed="false">
            <shape android:shape="line">
                <stroke android:width="1dp" android:color="@android:color/holo_red_dark"/>
            </shape>
        </item>
    </selector>
    

    Now, you just have to set above drawable in your TextView as foreground. For example,

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Your Textview with StrikeThrough"
        android:foreground="@drawable/strikethrough_foreground" />  <!-- this is available above --!>
    

    Through strings.xml

    In this method, you have to declare your string in strings.xml as strike through as,

    <string name="strike_line"> <strike>This line is strike throughed</strike></string>
    

    Note

    But I recommend you to strike through your TextView by setting foreground drawable. Because through drawable you can easily set your strike-through line color(as like I set as red color in above example) or size or any other style property. While in the other two methods default text color is strikethrough color.

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