Para dibujar un subrayado debajo de TextView en Android
Quiero dibujar el subrayado debajo de mi TextView
. He buscado algunos contenidos pero no pude encontrar nada fructífero.
¿Puede alguien por favor ayudarme aquí?
- Mostrar todos los caracteres Unicode en TextView
- Nuevo Texto Seleccionable en el componente android 3 (API <= 11)
- TextView`s LinkMovementMethod bloquea el evento de toque listitem
- Gridview multilínea Vista de texto cortada
- EditText Alternativas en un Widget? Tiene que haber una forma
- El texto no ellipsiza
- Copiar texto de TextView en Android
- IllegalArgumentException al seleccionar texto en Android TextView
- Android textview relleno entre líneas
- Android: Uso de alignBaseline para una imagen que sigue al texto
- Haga que el contenido de textview contenga texto en negrita y regular
- Android: LayoutParams para TextView hace que la vista desaparezca, programáticamente
- Evitar el empalme de línea no deseado en TextView
Hay tres maneras de subrayar el texto en TextView.
-
SpannableString
-
SetPaintFlags (); De TextView
-
Html.fromHtml ();
Permítanme explicarles todos los enfoques:
1er Enfoque
Para subrayar el texto en TextView tienes que usar SpannableString
String udata="Underlined Text"; SpannableString content = new SpannableString(udata); content.setSpan(new UnderlineSpan(), 0, udata.length(), 0); mTextView.setText(content);
2º Enfoque
Puede utilizar el método setPaintFlags de TextView para subrayar el texto de TextView.
Por ejemplo,
mTextView.setPaintFlags(mTextView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG); mTextView.setText("This text will be underlined");
Puede referirse a las constantes de la clase Paint si desea pulsar a través del texto.
3er Enfoque
Hacer uso de Html.fromHtml(htmlString);
String htmlString="<u>This text will be underlined</u>"; mTextView.setText(Html.fromHtml(htmlString));
O
txtView.setText(Html.fromHtml("<u>underlined</u> text"));
Sólo rodea el texto con la etiqueta <u> en el archivo de recursos string.xml
<string name="your_string"><u>Underlined text</u></string>
Y en su Actividad / Fragmento
mTextView.setText(R.string.your_string);
Su trabajo para mí.
tv.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG);
Para cualquier persona que todavía mira este querstion. Esto es para un hipervínculo pero se puede modificar para un simple subrayado:
Crear un drawable (hyperlink_underline.xml):
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:top="-10dp" android:left="-10dp" android:right="-10dp"> <shape android:shape="rectangle"> <solid android:color="@android:color/transparent"/> <stroke android:width="2dp" android:color="#3498db"/> </shape> </item> </layer-list>
Crear un nuevo estilo:
<style name="Hyperlink"> <item name="android:textColor">#3498db</item> <item name="android:background">@drawable/hyperlink_underline</item> </style>
A continuación, utilice este estilo en su TextView:
<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" local:MvxBind="Text Id; Click ShowJobInfoCommand" style="@style/HyperLink"/>
Si su TextView tiene ancho fijo, la solución alternativa puede ser crear una Vista que se verá como un subrayado y la posicionará justo debajo de su TextView.
<RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/myTextView" android:layout_width="20dp" android:layout_height="wrap_content"/> <View android:layout_width="20dp" android:layout_height="1dp" android:layout_below="@+id/myTextView" android:background="#CCCCCC"/> </RelativeLayout>
- Android – dibujable con esquinas redondeadas en la parte superior solamente
- Android: obtener la ubicación actual del usuario sin utilizar gps o Internet