如何使文本视图完全像这样

时间:2016-02-11 19:20:27

标签: android textview

尝试构建购物车并希望显示折扣价格和旧价格想要以图像形式显示

enter image description here

5 个答案:

答案 0 :(得分:2)

Dinamically(在运行时)你需要做:

TextView myTextView= (TextView) findViewById(R.id.some_text_view); 
myTextView.setText("myOldPriceHere");
myTextView.setPaintFlags(myTextView.getPaintFlags()| Paint.STRIKE_THRU_TEXT_FLAG);

答案 1 :(得分:1)

DO This:

   TextView someTextView = (TextView) findViewById(R.id.some_text_view); 
   someTextView.setText(someString); // SomeString = your old price
   someTextView.setPaintFlags(someTextView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);

答案 2 :(得分:1)

有两种选择:

使用TextViewPaint.STRIKE_THRU_TEXT_FLAG标记

  

绘制标记,将穿透装饰应用于绘制文本。

这是一个例子:

TextView someTextView = (TextView) findViewById(R.id.some_text_view);
someTextView.setText("$29,500");
myTextView.setPaintFlags(myTextView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);

enter image description here

或使用Spannable文字,这是一个例子:

TextView myTextView = (TextView) findViewById(R.id.my_text_view);
myTextView .setText("$29,500", TextView.BufferType.SPANNABLE);
final StrikethroughSpan STRIKE_THROUGH_SPAN = new StrikethroughSpan();
Spannable spannable = (Spannable) myTextView.getText();
spannable.setSpan(STRIKE_THROUGH_SPAN, 0, myTextView.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
myTextView.setText(spannable);

enter image description here

正如您所看到的,两个选项非常相似,因此您可以选择其中一个。

答案 3 :(得分:0)

>>> text = """In the third category he included those Brothers (the majority) who saw nothing in Freemasonry but the external forms and ceremonies, and prized the strict performance of these forms without troubling about their purport or significance. Such were Willarski and even the Grand Master of the principal lodge. Finally, to the fourth category also a great many Brothers belonged, particularly those who had lately joined. These according to Pierre's observations were men who had no belief in anything, nor desire for anything, but joined the Freemasons merely to associate with the wealthy young Brothers who were influential through their connections or rank, and of whom there were very many in the lodge.Pierre began to feel dissatisfied with what he was doing. Freemasonry, at any rate as he saw it here, sometimes seemed to him based merely on externals. He did not think of doubting Freemasonry itself, but suspected that Russian Masonry had taken a wrong path and deviated from its original principles. And so toward the end of the year he went abroad to be initiated into the higher secrets of the order.What is to be done in these circumstances? To favor revolutions, overthrow everything, repel force by force?No! We are very far from that. Every violent reform deserves censure, for it quite fails to remedy evil while men remain what they are, and also because wisdom needs no violence. "But what is there in running across it like that?" said Ilagin's groom. "Once she had missed it and turned it away, any mongrel could take it," Ilagin was saying at the same time, breathless from his gallop and his excitement. """
>>> answer = """In the third category he included those Brothers (the majority) who saw nothing in Freemasonry but the external forms and ceremonies, and prized the strict performance of these forms without troubling about their purport or significance.
... Such were Willarski and even the Grand Master of the principal lodge.
... Finally, to the fourth category also a great many Brothers belonged, particularly those who had lately joined.
... These according to Pierre's observations were men who had no belief in anything, nor desire for anything, but joined the Freemasons merely to associate with the wealthy young Brothers who were influential through their connections or rank, and of whom there were very many in the lodge.
... Pierre began to feel dissatisfied with what he was doing.
... Freemasonry, at any rate as he saw it here, sometimes seemed to him based merely on externals.
... He did not think of doubting Freemasonry itself, but suspected that Russian Masonry had taken a wrong path and deviated from its original principles.
... And so toward the end of the year he went abroad to be initiated into the higher secrets of the order.
... What is to be done in these circumstances?
... To favor revolutions, overthrow everything, repel force by force?
... No!
... We are very far from that.
... Every violent reform deserves censure, for it quite fails to remedy evil while men remain what they are, and also because wisdom needs no violence.
... "But what is there in running across it like that?" said Ilagin's groom.
... "Once she had missed it and turned it away, any mongrel could take it," Ilagin was saying at the same time, breathless from his gallop and his excitement."""
>>> 
>>> output = text.split('\n')
>>> sum(1 for sent in text.split('\n') if sent in answer)
0

答案 4 :(得分:0)

您可以使用Spannable来实现这一点,它更灵活,以防您只需将其放在文本的一部分中。这是一个例子:

textview.setText("29,500", TextView.BufferType.SPANNABLE);
final StrikethroughSpan STRIKE_THROUGH_SPAN = new StrikethroughSpan();
Spannable spannable = (Spannable) textview.getText();
spannable.setSpan(STRIKE_THROUGH_SPAN, 0, textview.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textview.setText(spannable);