2010-07-19 7 views

Répondre

313

Une autre réponse serait très similaire, mais ne serait pas nécessaire de mettre le texte de la TextView deux fois

TextView TV = (TextView)findViewById(R.id.mytextview01); 

Spannable wordtoSpan = new SpannableString("I know just how to whisper, And I know just how to cry,I know just where to find the answers");   

wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 15, 30, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 

TV.setText(wordtoSpan); 
+0

mais comment puis-je changer la couleur pour plusieurs mots je tout le texte pas une envergure? –

+0

@mostafahashim créer plusieurs travées en répétant la ligne 3 wordtoSpan.setSpan (nouveau ForegroundColorSpan (Color.RED), 50, 80, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); –

16

Définir votre texte de TextView spannable et définir un ForegroundColorSpan pour votre texte.

TextView textView = (TextView)findViewById(R.id.mytextview01);  
Spannable wordtoSpan = new SpannableString("I know just how to whisper, And I know just how to cry,I know just where to find the answers");   
wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 15, 30, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
textView.setText(wordtoSpan); 
+0

Merci! Est-il possible de le faire sans affecter le texte au TextView en premier? – hpique

+0

Je ne me suis pas bien expliqué.Laissez-moi reformuler. Les trois premières lignes sont-elles nécessaires? Vous ne pouvez pas créer directement l'objet Spannable à partir de la chaîne? – hpique

+0

Nop, vous devez stocker le texte de votre TextView dans un Tampon Spannable pour changer la couleur de premier plan. – Jorgesys

71

Voici une petite fonction d'aide. Idéal pour quand vous avez plusieurs langues!

private void setColor(TextView view, String fulltext, String subtext, int color) { 
    view.setText(fulltext, TextView.BufferType.SPANNABLE); 
    Spannable str = (Spannable) view.getText(); 
    int i = fulltext.indexOf(subtext); 
    str.setSpan(new ForegroundColorSpan(color), i, i + subtext.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
} 
+0

non, vous pouvez faire ce que vous voulez avec du HTML. –

5

Il y a une usine pour créer la Spannable, et éviter le casting, comme celui-ci:

Spannable span = Spannable.Factory.getInstance().newSpannable("text"); 
+1

Pourriez-vous clarifier comment utiliser SpannableFactory? A quoi devrait ressembler le "texte"? – Piotr

+0

après avoir créé le Spannable par SpannableFactory, alors comment l'utiliser? – MBH

12

Une autre façon qui pourrait être utilisé dans certaines situations est de définir la couleur de lien dans les propriétés du vue qui prend le Spannable.

Si votre Spannable va être utilisé dans un TextView, par exemple, vous pouvez définir la couleur de lien dans le fichier XML comme ceci:

<TextView 
    android:id="@+id/myTextView" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:textColorLink="@color/your_color" 
</TextView> 

Vous pouvez également définir dans le code avec:

TextView tv = (TextView) findViewById(R.id.myTextView); 
tv.setLinkTextColor(your_color); 
23

Si vous souhaitez plus de contrôle, vous pouvez vérifier la classe TextPaint.Voici comment l'utiliser:

final ClickableSpan clickableSpan = new ClickableSpan() { 
    @Override 
    public void onClick(final View textView) { 
     //Your onClick code here 
    } 

    @Override 
    public void updateDrawState(final TextPaint textPaint) { 
     textPaint.setColor(yourContext.getResources().getColor(R.color.orange)); 
     textPaint.setUnderlineText(true); 
    } 
}; 
+0

getColor (int id) est obsolète sur Android 6.0 Marshmallow (API 23) https://stackoverflow.com/questions/31590714/getcolorint-id-deprecated-on-android-6-0-marshmallow-api-23 –

4

Set Couleur sur Texte par passant cordes et couleur:

private String getColoredSpanned(String text, String color) { 
    String input = "<font color=" + color + ">" + text + "</font>"; 
    return input; 
} 

Set texte sur TextView/Bouton/EditText etc en appelant le code ci-dessous:

TextView:

TextView txtView = (TextView)findViewById(R.id.txtView); 

Get couleur Chaîne:

String name = getColoredSpanned("Hiren", "#800000"); 

Set Texte sur TextView:

txtView.setText(Html.fromHtml(name)); 

Terminé

16

Je trouve toujours des exemples visuels utiles lorsque j'essaie de comprendre un nouveau concept.

couleur de fond

enter image description here

SpannableString spannableString = new SpannableString("Hello World!"); 
BackgroundColorSpan backgroundSpan = new BackgroundColorSpan(Color.YELLOW); 
spannableString.setSpan(backgroundSpan, 0, spannableString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
textView.setText(spannableString); 

Couleur de premier plan

enter image description here

SpannableString spannableString = new SpannableString("Hello World!"); 
ForegroundColorSpan foregroundSpan = new ForegroundColorSpan(Color.RED); 
spannableString.setSpan(foregroundSpan, 0, spannableString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
textView.setText(spannableString); 

Combinaison

enter image description here

SpannableString spannableString = new SpannableString("Hello World!"); 
ForegroundColorSpan foregroundSpan = new ForegroundColorSpan(Color.RED); 
BackgroundColorSpan backgroundSpan = new BackgroundColorSpan(Color.YELLOW); 
spannableString.setSpan(foregroundSpan, 0, 8, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
spannableString.setSpan(backgroundSpan, 3, spannableString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
textView.setText(spannableString); 

une étude plus approfondie

0
  1. créer textview dans ur mise en page
  2. coller ce code dans ur MainActi tivité

    TextView textview=(TextView)findViewById(R.id.textviewid); 
    Spannable spannable=new SpannableString("Hello my name is sunil"); 
    spannable.setSpan(new ForegroundColorSpan(Color.BLUE), 0, 5, 
    Spannable.SPAN_INCLUSIVE_EXCLUSIVE); 
    textview.setText(spannable); 
    //Note:- the 0,5 is the size of colour which u want to give the strring 
    //0,5 means it give colour to starting from h and ending with space i.e.(hello), if you want to change size and colour u can easily 
    
0
private SpannableString spannableString(SpannableString spannableString, int start, int end) { 
    ColorStateList redColor = new ColorStateList(new int[][]{new int[]{}}, new int[]{0xffa10901}); 
    TextAppearanceSpan highlightSpan = new TextAppearanceSpan(null, Typeface.BOLD, -1, redColor, null); 

    spannableString.setSpan(highlightSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
    spannableString.setSpan(new BackgroundColorSpan(0xFFFCFF48), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
    spannableString.setSpan(new RelativeSizeSpan(1.5f), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 

    return spannableString; 
} 

SpannableString spannableString = new SpannableString("I don't like Hasina."); 
spannableString(spannableString, 8, 14); 
textView.setText(spannableString); 

Sortie:

enter image description here

Questions connexes