Comment définir le style de police en gras, italique et souligné dans un Android TextView?

StackOverflow https://stackoverflow.com/questions/4623508

  •  30-09-2019
  •  | 
  •  

Question

Je veux faire un contenu de TextView gras, italique et souligné. J'ai essayé le code suivant et il fonctionne, mais ne souligne pas.

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

Comment puis-je faire? Des idées rapides?

Était-ce utile?

La solution

Je ne sais pas souligné, mais pour le gras et en italique il y a "bolditalic". Il n'y a pas mention souligné ici: http://developer.android .com / référence / android / widget / TextView.html # attr_android: TEXTSTYLE

Rappelez-vous que d'utiliser le bolditalic dit que vous avez besoin, et je cite cette page

  

doit être un ou plusieurs (séparés par des « | »). Des valeurs constantes suivantes

donc vous devriez utiliser bold|italic

Vous pouvez vérifier cette question pour underline: Puis-je souligné dans le texte un androïde mise en page?

Autres conseils

Cela devrait rendre votre TextView gras , souligné et italique 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 JAVA ,

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

Parfois, l'approche ci-dessus ne sera pas utile lorsque vous pourriez avoir à utiliser 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

entrer image description ici

Ou 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);

Keep it simple et en une ligne:)

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>

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

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

Programmatialy:

Vous pouvez le faire en utilisant la méthode de programmation setTypeface ():

Voici le code pour 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 dans comme:

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

sans les guillemets fonctionne pour moi:

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

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 que les processus HTML chaînes en texte Styled affichable.

// textString is the String after you retrieve it from the file
textView.setText(Html.fromHtml(textString));
    style="?android:attr/listSeparatorTextViewStyle
  • en faisant ce style, u peut atteindre soulignage
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top