creating a strikethrough text?

前端 未结 11 938
借酒劲吻你
借酒劲吻你 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: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); 
    

提交回复
热议问题