la création d'un texte barré?

puis-je créer un texte strikethrough sur Android, je veux dire ajouter une valeur spéciale dans la balise TextView qui peut rendre cela possible?

<TextView
    android:id="@+id/title" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:textColor="#040404"
    android:typeface="sans" 
    android:textSize="12dip"
    android:textStyle="bold"/>
85
demandé sur Kling Klang 2012-03-20 16:14:56

8 réponses

"151920920 de la Peinture".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);

pour la peinture de texte, il y a plusieurs drapeaux de bits pour faire des choses comme le gras, l'italique, et oui barré. Afin de permettre à l'barré, vous devez retourner les bits qui correspond à ce drapeau. La méthode la plus simple la façon de le faire est d'utiliser un bitwise-ou sur les drapeaux actuels et un constante qui correspond à un ensemble de pavillons avec seulement la barré indicateur activé.

244
répondu hovanessyan 2012-03-20 12:20:35

essayez ceci:

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); 
18
répondu ρяσѕρєя K 2012-03-20 12:18:47

c'est vraiment facile si vous utilisez des chaînes de caractères:

<string name="line"> Not crossed <strike> crossed </strike> </string>

et puis juste:

<TextView 
        ...
         android:text="@string/line"
 />
14
répondu Ignacio Alorre 2014-06-07 00:35:37

Je ne fais que copier ma réponse . J'espère que ça aidera quelqu'un Si vous avez un seul mot, nous pouvons utiliser drawable. Voici l'exemple:

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

si vous avez plusieurs lignes, vous pouvez utiliser le code suivant:

TextView someTextView = (TextView) findViewById(R.id.some_text_view);
someTextView.setText(someString);
someTextView.setPaintFlags(someTextView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG)
11
répondu Akshay Mukadam 2017-05-23 12:18:11

il suffit d'utiliser ceci et vous êtes fait . Pour Activité:

TextView t= (TextView).findViewById(R.id.thousand));
t.setPaintFlags(t.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);

Pour Xml:

<RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/text_view_original_cash_amount"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="5dp"
                android:textColor="@android:color/darker_gray"
                android:text="Rs. 1,999"/>

            <View
                android:layout_width="wrap_content"
                android:layout_height="1dp"
                android:background="@android:color/darker_gray"
                android:layout_centerVertical="true"
                android:layout_alignStart="@id/text_view_original_cash_amount"
                android:layout_alignEnd="@id/text_view_original_cash_amount"
                android:layout_alignLeft="@id/text_view_original_cash_amount"
                android:layout_alignRight="@id/text_view_original_cash_amount" /> 
</RelativeLayout>
3
répondu Hanisha 2018-03-26 10:02:16

vous pouvez le faire de trois façons, soit en mettant l'avant-plan dans TextView , soit en mettant PaintFlag ou en déclarant la chaîne de caractères comme <strike>your_string</strike> dans strings.xml . Par exemple,

Par PaintFlag

c'est la méthode la plus simple que vous avez juste à mettre strikethrough drapeau sur votre TextView comme,

yourTextView.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG);

il va frapper à travers votre TextView.

par tirage au premier plan

vous pouvez également frapper à travers votre TextView en mettant un avant-plan comme,

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

Maintenant, vous avez juste à définir au-dessus de drawable dans votre TextView comme foreground . Par exemple,

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

par cordes.xml

dans cette méthode, vous devez déclarer votre chaîne dans strings.xml comme biffer comme,

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

Note

mais je vous recommande de passer à travers votre TextView en mettant avant-plan drawable. Parce que grâce à drawable vous pouvez facilement définir votre grève par la couleur de ligne (comme j'ai défini la couleur rouge dans l'exemple ci-dessus) ou la taille ou toute autre propriété de style. Tandis que dans les deux autres méthodes par défaut textcolor est frappé par la couleur.

2
répondu HeisenBrg 2018-08-07 03:02:09

j'ai essayé peu d'options ci-dessus, mais, cela fonctionne le mieux pour moi:

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

cheers

1
répondu Milos Simic Simo 2018-01-16 22:34:20

si vous utilisez Kotlin:

your_text_view.apply {
    paintFlags = paintFlags or Paint.STRIKE_THRU_TEXT_FLAG
    text = "Striked thru text"
}
0
répondu bko 2018-09-15 11:59:25