2014-06-08 11 views
5

Je veux faire Habit nombre: gras, j'ai essayé le tag HTML, mais cela n'a pas fonctionné. J'ai fait des recherches mais je n'en ai pas trouvé. J'espère que quelqu'un est capable de m'aider. Merci!Faire une chaîne texte Gras en Java Android

String habitnumber = "Habit Number: " + String.valueOf(habitcounter); 
String Title = habit.getTitle(); 
String description = habit.getDescription(); 


//Set text for the row 
       tv.setText(habitnumber+ "\n" + Title + " \n" + description + "\n --------------------"); 
+0

Comment allez-vous éventuellement l'afficher à l'écran? Cela fait une grande différence –

+0

@GabeSechan édité ma question, voir maintenant – meskh

+1

Vous devez le définir dans un spannable avec un style audacieux. Alternativement, vous pouvez ajouter les balises html en gras puis utiliser Html.fromHtml pour obtenir une chaîne avec des spannables formés à partir du html –

Répondre

15
I tried the HTML tag but it didn't work 

Je pense que tu na pas envelopper votre chaîne à Html.fromHtml

solution :

Vous pouvez le faire en HTML en utilisant le tag html pour gras: <b>

String habitnumber = "<b>" + "Habit Number: " + "</b> " + String.valueOf(habitcounter); 
tv.setText(Html.fromHtml(habitnumber)); 
1
tv.setTypeface(null, Typeface.BOLD); 
+0

Le problème avec ceci est qu'il rend le texte entier en gras. Ce qui n'est peut-être pas ce qu'il veut. –

+0

alors peut-être textview en textview? quel problème ? – JaKoZo

0

Comme Gabe mentionné, essayez d'utiliser l'utilisation Html.fromHtml ou utilisez spannable. Check this out: Selecting, Highlighting, or Styling Portions of Text

Vous pouvez utiliser essentiellement les balises HTML dans votre ressource de chaîne comme:

<resource> 
<string id="@+id/styled_welcome_message"><b>your text</b></string> 
</resources> 
0

Utilisez ce dans le fichier XML, dans le textview

android:textStyle="bold" 
Questions connexes