Come impostare lo stile del carattere in grassetto, corsivo e sottolineato in un TextView Android?

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

  •  30-09-2019
  •  | 
  •  

Domanda

Voglio rendere i contenuti audace di un TextView, corsivo e sottolineato. Ho provato il seguente codice e funziona, ma non sottolinea.

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

Come posso fare? Tutte le idee veloce?

È stato utile?

Soluzione

Non so su sottolineatura, ma per grassetto e corsivo c'è "bolditalic". Non v'è alcuna menzione di sottolineatura qui: http://developer.android .com / riferimento / android / widget di / TextView.html # attr_android: TEXTSTYLE

Mente che per utilizzare il bolditalic menzionata è necessario, e cito da quella pagina

  

deve essere uno o più (separati da '|'). Dei valori seguenti costanti

in modo usereste bold|italic

Si potrebbe verificare questa domanda per la sottolineatura: posso testo sottolineato in un androide layout?

Altri suggerimenti

Questo dovrebbe rendere la vostra TextView grassetto , sottolineata e corsivo , allo stesso tempo.

strings.xml

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

Per impostare questa stringa alla TextView, fare questo nella tua 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" />

o in JAVA ,

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

A volte l'approccio di cui sopra non sarà utile quando si potrebbe essere necessario utilizzare testo dinamico. Quindi, in questo caso SpannableString entra in azione.

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

USCITA

entrare descrizione dell'immagine qui

O semplicemente come questo in 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

O in 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);

fare cose semplici e in una sola riga:)

Per grassetto e corsivo ciò che si sta facendo è corretto per l'utilizzo di sottolineatura seguente codice

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>

Questo è un modo semplice per aggiungere una sottolineatura, mantenendo le altre impostazioni:

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

Programmatialy:

Si può fare di programmazione utilizzando il metodo setTypeface ():

Di seguito è riportato il codice per Tipo di carattere predefinito

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

e se si desidera impostare Carattere personalizzato:

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:

È possibile impostare direttamente nel file XML dentro come:

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

senza virgolette opere per me:

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

Se stai leggendo che il testo da un file o dalla rete.

E 'possibile realizzarlo con l'aggiunta di tag HTML per il testo, come accennato

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

e quindi è possibile utilizzare la classe HTML che i processi HTML stringhe in testo in stile visualizzabile.

// textString is the String after you retrieve it from the file
textView.setText(Html.fromHtml(textString));
    style="?android:attr/listSeparatorTextViewStyle
  • , rendendo questo stile, u può raggiungere sottolineatura
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top