Establecer el estilo TextView (en negrita o en cursiva)
¿Cómo configurar el estilo de TextView
(negrita o itálica) con en Java y sin usar el diseño XML?
En otras palabras, necesito escribir android:textStyle
con Java.
- Cómo hacer que el TextView arrastre en LinearLayout suave, en android?
- El interfuncionamiento de TextView
- ¿Qué es android: includeFontPadding?
- Cómo animar el textview (texto muy largo) desplazarse automáticamente horizontalmente
- Etiqueta: TextLayoutCache Texto: Valor de caché eliminado
- Configuración de textview en la parte inferior de android
- Cómo cambiar el color de las palabras con hashtags
- Android TagHandler sin efecto en las etiquetas estándar
- Cómo encontrar texto visible en la vista de texto
- Android - Extensible TextView con animación
- Configuración de textIsSelectable en TextView con el dellipsize de elimina elimina elipse
- Manejo de varios ClickableSpan en un TextView
- Cómo alinear mediante programación un Android TextView a la derecha de otro TextView
textView.setTypeface(null, Typeface.BOLD_ITALIC); textView.setTypeface(null, Typeface.BOLD); textView.setTypeface(null, Typeface.ITALIC); textView.setTypeface(null, Typeface.NORMAL);
Usa lo que quieras.
Pruebe esto para establecer en TextView
para negrita o cursiva
textView.setTypeface(textView.getTypeface(), Typeface.BOLD); textView.setTypeface(textView.getTypeface(), Typeface.ITALIC); textView.setTypeface(textView.getTypeface(), Typeface.BOLD_ITALIC);
Programáticamente:
Puede hacer mediante programación usando setTypeface()
:
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
XML:
Puede establecer directamente en el archivo XML en <TextView />
como:
android:textStyle="normal" android:textStyle="normal|bold" android:textStyle="normal|italic" android:textStyle="bold" android:textStyle="bold|italic"
Tiene dos opciones:
Opción 1 (sólo funciona en negrita, cursiva y subrayado):
String s = "<b>Bolded text</b>, <i>italic text</i>, even <u>underlined</u>!" TextView tv = (TextView)findViewById(R.id.THE_TEXTVIEW_ID); tv.setText(Html.fromHtml(s));
Opcion 2:
Utilice un Spannable ; Es más complicado, pero puede modificar dinámicamente los atributos de texto (no sólo negrita / cursiva, también colores).
Programáticamente:
Puede hacer mediante programación utilizando el método setTypeface()
:
A continuación se muestra el código de 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 configurar Custom Typeface :
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:
Puede establecer directamente en el archivo XML en <TextView />
esta manera:
android:textStyle="normal" android:textStyle="normal|bold" android:textStyle="normal|italic" android:textStyle="bold" android:textStyle="bold|italic"
TextView text = (TextView)findViewById(R.id.THE_TEXTVIEW_ID);
Ahora configura las propiedades de vista de texto.
text.setTypeface(null, Typeface.BOLD); //-- for only bold the text text.setTypeface(null, Typeface.BOLD_ITALIC); //-- for bold & italic the text text.setTypeface(null, Typeface.ITALIC); // -- for italic the text
TextView
esto para establecer tu estilo de TextView
por código java
txt1.setTypeface(null,Typeface.BOLD_ITALIC);
TextView text = (TextView)findViewById(R.layout.textName); text.setTypeface(null,Typeface.BOLD);
Podría ser
yourTextView.setTypeface(null,Typeface.DEFAULT_BOLD);
Y la cursiva debe ser capaz de reemplazar Typeface.DEFAULT_BOLD
con Typeface.DEFAULT_ITALC
.
Déjame saber cómo funciona.
Use textView.setTypeface(Typeface tf, int style);
Para establecer la propiedad de estilo de TextView
. Consulte la documentación del desarrollador para obtener más información.
Prueba esto:
TextView textview = (TextView)findViewById(R.id.textview_idname); textview.setTypeface(null,Typeface.BOLD);
Simplemente si desea hacer texto en negrita . Escriba esta línea en su diseño en la propiedad de vista de texto
android:textStyle="bold"
La forma estándar de hacerlo es utilizar los estilos personalizados. Ex-
En styles.xml
añada lo siguiente.
<resources xmlns:android="http://schemas.android.com/apk/res/android"> <style name="MyApp.TextAppearance.LoginText"> <item name="android:textStyle">bold|italic</item> </style>
Aplique este estilo a su TextView
siguiente manera.
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" style="@style/MyApp.TextAppearance.LoginText" />
Y como se explica aquí Android Developers String Resources si necesita usar parámetros en su recurso de estilo de texto, tiene que escapar de los corchetes de apertura
<resources> <string name="welcome_messages">Hello, %1$s! You have <b>%2$d new messages</b>.</string> </resources>
Y el formato de la llamada Html (cadena)
Resources res = getResources(); String text = String.format(res.getString(R.string.welcome_messages), username, mailCount); CharSequence styledText = Html.fromHtml(text);
Desde que quiero utilizar una fuente personalizada sólo conjunción de varias respuestas funciona para mí. Obviamente los ajustes en mi layout.xml
como android:textStlyle="italic"
fueron ignorados por AOS. Así que finalmente tuve que hacer lo siguiente: en strings.xml
la cadena de destino se declaró como:
<string name="txt_sign"><i>The information blah blah ...</i></string>
Entonces adicionalmente en código:
TextView textSign = (TextView) findViewById(R.id.txt_sign); FontHelper.setSomeCustomFont(textSign); textSign.setTypeface(textSign.getTypeface(), Typeface.ITALIC);
No probé la opción Spannable
(que supongo que DEBE trabajar) pero
textSign.setText(Html.fromHtml(getString(R.string.txt_sign)))
No tuvo ningún efecto. También si quito la italic tag
de strings.xml
dejando el setTypeface()
todo solo no tiene ningún efecto tampoco. Tricky Android …
La mejor manera es definirlo en styles.xml
<style name="common_txt_style_heading" parent="android:style/Widget.TextView"> <item name="android:textSize">@dimen/common_txtsize_heading</item> <item name="android:textColor">@color/color_black</item> <item name="android:textStyle">bold|italic</item> </style>
Y actualizarlo en TextView
<TextView android:id="@+id/txt_userprofile" style="@style/common_txt_style_heading" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="@dimen/margin_small" android:text="@string/some_heading" />
AppCompatTextView text =(AppCompatTextView)findViewById(R.layout.appCompatTextView1); text.setTypeface(null,Typeface.BOLD);
Utilice el método anterior para establecer el tipo de letra mediante programación.
Una manera de hacerlo es:
myTextView.setTypeface(null, Typeface.ITALIC); myTextView.setTypeface(null, Typeface.BOLD_ITALIC); myTextView.setTypeface(null, Typeface.BOLD); myTextView.setTypeface(null, Typeface.NORMAL);
Otra opción si desea conservar el tipo de letra anterior y no desea perder previamente aplicado entonces:
myTextView.setTypeface(textView.getTypeface(), Typeface.NORMAL); myTextView.setTypeface(textView.getTypeface(), Typeface.BOLD); myTextView.setTypeface(textView.getTypeface(), Typeface.ITALIC); myTextView.setTypeface(textView.getTypeface(), Typeface.BOLD_ITALIC);
Puede probar de esta manera:
<string name="title"><u><b><i>Your Text</i></b></u></string>
- Actividad de reinicio en la rotación de Android
- ¿Qué significa este mensaje de error de publicación de Google Play APK?