Cómo modificar el estilo de fuente en negrita, cursiva y subrayado en una TextView Android?
-
30-09-2019 - |
Pregunta
Quiero hacer que el contenido de una negrita TextView
, cursiva y subrayado. He probado el siguiente código y funciona, pero no subraya.
<Textview android:textStyle="bold|italic" ..
¿Cómo lo hago? Cualquier ideas rápidas?
Solución
No sé acerca de subrayado, pero para negrita y cursiva no es "bolditalic"
. No hay ninguna mención de subrayado aquí: http://developer.android .com / referencia / android / Widget / TextView.html # attr_android: TEXTSTYLE
Eso sí que usar la bolditalic
mencionado es necesario, y cito de la página
debe ser uno o más (separados por '|'). De los valores siguientes constantes
por lo que tendría que utilizar bold|italic
Se pudo comprobar esta pregunta para subrayado: ¿Puedo texto subrayado en un androide diseño?
Otros consejos
Esto debería hacer que su TextView negrita ??strong>, subrayado y cursiva al mismo tiempo.
strings.xml
<resources>
<string name="register"><u><b><i>Copyright</i></b></u></string>
</resources>
Para establecer esta cadena a la Vista de Texto, hacer esto en su 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 en JAVA ,
TextView textView = new TextView(this);
textView.setText(R.string.register);
A veces el enfoque anterior no será útil cuando puede que tenga que utilizar texto dinámico. Así que en ese caso SpannableString entra en acción.
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);
SALIDA
O simplemente como esto en 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 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);
Debe ser sencillo y en una sola línea:)
En negrita y cursiva lo que está haciendo es correcto para el uso de subrayado siguiente código
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>
Esta es una manera fácil de agregar un subrayado, manteniendo al mismo tiempo otros ajustes:
textView.setPaintFlags(textView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
Programmatialy:
Se puede hacer mediante programación utilizando el método setTypeface ():
A continuación se muestra el código para el tipo de letra predeterminado
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
y si desea establecer Tipo de letra personalizado:
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:
Se puede configurar directamente en el archivo XML en como:
android:textStyle="normal"
android:textStyle="normal|bold"
android:textStyle="normal|italic"
android:textStyle="bold"
android:textStyle="bold|italic"
sin comillas funciona para mí:
<item name="android:textStyle">bold|italic</item>
Si usted está leyendo este texto desde un archivo o desde la red.
Se puede lograr mediante la adición de etiquetas HTML para el texto, como se mencionó
This text is <i>italic</i> and <b>bold</b>
and <u>underlined</u> <b><i><u>bolditalicunderlined</u></b></i>
y entonces usted puede utilizar la clase HTML que los procesos HTML cuerdas en texto con estilo visualizable.
// textString is the String after you retrieve it from the file
textView.setText(Html.fromHtml(textString));
style="?android:attr/listSeparatorTextViewStyle
- al hacer este estilo, u puede lograr subrayado