Alineación en Html.fromHtml ()

¿Está funcionando la alineación en HTML.fromHtml () en un TextView?

Lo intenté

<div align="center"><p>Test</p></div> 

Y algunas variaciones de esto, incluyendo poner las lengüetas de la alineación sin paréntesis a la etiqueta del párrafo, pero ninguna trabajada. El texto siempre queda.

¡Gracias por cualquier ayuda!

Tuya.

La alineación no se admite en texto HTML en TextView.

Aunque es posible utilizar la alineación utilizando una vista web en lugar de un TextView, y cargar el html desde un archivo colocado en activos:

 public class ViewWeb extends Activity { WebView webview; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.webview); webview = (WebView) findViewById(R.id.webView1); webview.loadUrl("file:///android_asset/index.html"); } } 

Para establecer la alineación en textview, establezca primero el ancho de texto en match_parent y luego use la opción de alineación de texto en su etiqueta HTML. algo como esto:

<div style="text-align: center">this text should be in center of view</div>

Actualización: Esta solución sólo funciona en android 7 y superior: |

No puede establecer la alineación en texto HTML, pero puede utilizar SpannableStringBuilder en su lugar. Es detallado pero consigue el trabajo hecho.

P.ej

 private Spanned getFormattedLabelText(String text, String subText) { String fullText = String.format("%s\n%s", text, subText); int fullTextLength = fullText.length(); int titleEnd = text.length(); SpannableStringBuilder s = new SpannableStringBuilder(fullText); //Right align the text AlignmentSpan alignmentSpan = new AlignmentSpan.Standard(Layout.Alignment.ALIGN_OPPOSITE); s.setSpan(alignmentSpan, 0, fullTextLength, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //Make the title bold s.setSpan(new StyleSpan(Typeface.BOLD), 0, titleEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //bold //make the subtext small int smallTextSize = DisplayUtil.getPixels(TypedValue.COMPLEX_UNIT_SP, 10); s.setSpan(new AbsoluteSizeSpan(smallTextSize), titleEnd + 1, fullTextLength, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //bold return s; } 

A continuación, establezca el texto TextView como de costumbre:

 myTextView.setText(getFormattedLabelText("Title", "Subtitle")); 

Creo que es más fácil utilizar la propiedad Gravity en un TextView para centrar el texto …

FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.