2011-01-07 5 views
334

Je souhaite que le contenu de TextView soit en gras, italique et souligné. J'ai essayé le code suivant et cela fonctionne, mais ne souligne pas.Comment définir le style de police en gras, italique et souligné dans un TextView Android?

<Textview android:textStyle="bold|italic" .. 

Comment faire? Des idées rapides?

+0

ça marche à mettre juste un d'entre eux? – falstro

+0

oui fonctionne bien je veux aussi faire en ligne. –

+5

textView.setPaintFlags (Paint.UNDERLINE_TEXT_FLAG); – bCliks

Répondre

211

Je ne connais pas le soulignement, mais pour le gras et l'italique, il y a "bolditalic". Il n'y a pas mention souligné ici: http://developer.android.com/reference/android/widget/TextView.html#attr_android:textStyle

Rappelez-vous que pour utiliser l'Mentionné bolditalic vous avez besoin, et je cite cette page

Doit être un ou plusieurs (séparés par « | ») de les valeurs constantes suivantes.

donc vous devriez utiliser bold|italic

Vous pouvez vérifier cette question pour underline: Can I underline text in an android layout?

+43

pour la ligne en-dessous .. 'textView.setPaintFlags (Paint.UNDERLINE_TEXT_FLAG);' – bCliks

+1

@bala soyez conscient que votre solution souligne toujours le texte entier, donc ce n'est pas faisable dans les cas où l'on veut en souligner seulement une partie. –

62

Pour en gras et en italique tout ce que vous faites est correct pour une utilisation underscore code suivant

HelloAndroid. java

package com.example.helloandroid; 

import android.app.Activity; 
import android.os.Bundle; 
import android.text.SpannableString; 
import android.text.style.UnderlineSpan; 
import android.widget.TextView; 

public class HelloAndroid extends Activity { 
TextView textview; 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    textview = (TextView)findViewById(R.id.textview); 
    SpannableString content = new SpannableString(getText(R.string.hello)); 
    content.setSpan(new UnderlineSpan(), 0, content.length(), 0); 
    textview.setText(content); 
} 
} 

main.xml

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/textview" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:text="@string/hello" 
android:textStyle="bold|italic"/> 

string.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <string name="hello">Hello World, HelloAndroid!</string> 
    <string name="app_name">Hello, Android</string> 
</resources> 
+0

Pour supprimer la valeur Null de passage 'underline' au lieu de' new UnderlineSpan() 'comme suit' content.setSpan (null, 0, content.length(), 0); ' –

332

Cela devrait rendre votre TextView gras, souligné et italic en même temps.

strings.xml

<resources> 
    <string name="register"><u><b><i>Copyright</i></b></u></string> 
</resources> 

Pour définir cette chaîne à votre TextView, faites ceci dans votre main.xml

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/textview" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:text="@string/register" /> 

ou dans JAVA,

TextView textView = new TextView(this); 
textView.setText(R.string.register); 

Parfois, l'approche ci-dessus ne sera pas utile lorsque vous devrez utiliser du texte dynamique. Donc, dans ce cas, SpannableString entre en action.

String tempString="Copyright"; 
TextView text=(TextView)findViewById(R.id.text); 
SpannableString spanString = new SpannableString(tempString); 
spanString.setSpan(new UnderlineSpan(), 0, spanString.length(), 0); 
spanString.setSpan(new StyleSpan(Typeface.BOLD), 0, spanString.length(), 0); 
spanString.setSpan(new StyleSpan(Typeface.ITALIC), 0, spanString.length(), 0); 
text.setText(spanString); 

SORTIE

enter image description here

+0

Est-ce que cela fonctionne pour TOUS les niveaux d'API? – kape123

+3

Je l'ai vérifié dans 2.1.Donc, au moins, il devrait fonctionner à partir de 2.1 et au-dessus –

+3

La solution SpannableString ne fonctionne pas. – Phillip

18

sans les guillemets fonctionne pour moi:

<item name="android:textStyle">bold|italic</item> 
45

Ceci est un moyen facile d'ajouter un soulignement, tout en maintenant les autres paramètres:

textView.setPaintFlags(textView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG); 
+0

Sachez que cette solution souligne toujours le texte entier, donc ce n'est pas faisable dans les cas où l'on veut en souligner une partie. –

108

Ou tout simplement comme ça dans Kotlin:

val tv = findViewById(R.id.textViewOne) as TextView 
tv.setTypeface(null, Typeface.BOLD_ITALIC) 
// OR 
tv.setTypeface(null, Typeface.BOLD or Typeface.ITALIC) 
// OR 
tv.setTypeface(null, Typeface.BOLD) 
// OR 
tv.setTypeface(null, Typeface.ITALIC) 
// AND 
tv.paintFlags = tv.paintFlags or Paint.UNDERLINE_TEXT_FLAG 

Ou en Java:

TextView tv = (TextView)findViewById(R.id.textViewOne); 
tv.setTypeface(null, Typeface.BOLD_ITALIC); 
// OR 
tv.setTypeface(null, Typeface.BOLD|Typeface.ITALIC); 
// OR 
tv.setTypeface(null, Typeface.BOLD); 
// OR 
tv.setTypeface(null, Typeface.ITALIC); 
// AND 
tv.setPaintFlags(tv.getPaintFlags()|Paint.UNDERLINE_TEXT_FLAG); 

rester simple et en une ligne :)

+0

En insérant le package kotlinx.android.synthetic pour la vue avec laquelle vous travaillez, findViewByID n'est pas nécessaire dans Kotlin, rendant chacune des lignes setTypeface: textViewOne.setTypeface (...) – cren90

4
style="?android:attr/listSeparatorTextViewStyle 
  • en faisant ce style , vous pouvez réaliser le soulignement
16

Si vous lisez ce texte à partir d'un fichier ou du réseau.

Vous pouvez y parvenir en ajoutant des balises HTML à votre texte comme mentionné

This text is <i>italic</i> and <b>bold</b> 
and <u>underlined</u> <b><i><u>bolditalicunderlined</u></b></i> 

et vous pouvez utiliser la classe HTML qui traite les chaînes HTML en texte affichable style.

// textString is the String after you retrieve it from the file 
textView.setText(Html.fromHtml(textString)); 
+0

Thank! C'est très pratique pour moi que de le faire dans des fichiers XML. La méthode – mboy

+0

fromHtml (String) a été abandonnée au niveau de l'API 24 Plus de discussion ici: https://stackoverflow.com/questions/37904739/html-fromhtml-deprecated-in-android-n –

29

Programmatialy:

Vous pouvez le faire par programmation à l'aide setTypeface() méthode:

est Ci-dessous le code par défaut Typeface

textView.setTypeface(null, Typeface.NORMAL);  // for Normal Text 
textView.setTypeface(null, Typeface.BOLD);  // for Bold only 
textView.setTypeface(null, Typeface.ITALIC);  // for Italic 
textView.setTypeface(null, Typeface.BOLD_ITALIC); // for Bold and Italic 

et si vous souhaitez définir Typeface personnalisé:

textView.setTypeface(textView.getTypeface(), Typeface.NORMAL);  // for Normal Text 
textView.setTypeface(textView.getTypeface(), Typeface.BOLD);  // for Bold only 
textView.setTypeface(textView.getTypeface(), Typeface.ITALIC);  // for Italic 
textView.setTypeface(textView.getTypeface(), Typeface.BOLD_ITALIC); // for Bold and Italic 

XML:

Vous pouvez définir directement dans le fichier XML comme:

android:textStyle="normal" 
android:textStyle="normal|bold" 
android:textStyle="normal|italic" 
android:textStyle="bold" 
android:textStyle="bold|italic" 
Questions connexes