Android: html en strings.xml
Me gustaría mostrar, por ejemplo, este código html:
<body> <p><b>Hello World</b></p> <p>This is a test of the URL <a href="http://www.example.com"> Example</a></p> <p><b>This text is bold</b></p> <p><em>This text is emphasized</em></p> <p><code>This is computer output</code></p> <p>This is<sub> subscript</sub> and <sup>superscript</sup></p> </body>
Quiero mostrarlo en un diálogo declarando html en los recursos strings.xml
. ¿Cómo puedo hacerlo?
- Doctype Android lint.xml con declaración dtd
- Cómo detener la tarea Asyn en onPreExecute?
- Errores de procesamiento XML Vista previa de Android N
- Evita múltiples "%" caracteres en Android
- Collapsing ToolBar Layout siempre Colapsar si Mostrar PopUp Elemento de menú RecyclerView
- Android - java.lang.UnsupportedOperationException: No se puede convertir a dimension: type = 0x12
- Cómo generar simple-xml java objeto anotado desde el esquema xsd
- ((Botón)) dentro de un servicio no funciona
- Android: traducciones en recursos
- Android SAX Parsing: Cómo conservar Newlines desde dentro de una etiqueta CDATA
- Tag mismatch error-Android studio
- ¿Cuál es la mejor práctica para nombrar el ID de Elemento XML en Android?
- RecyclerView Varias vistas de disposición en una clase de adaptador
La mejor manera de añadir código fuente html en strings.xml es usar <![CDATA[html source code]]>
. Aquí hay un ejemplo:
<string name="html"><![CDATA[<p>Text<p>]]></string>
A continuación, puede mostrar este html en TextView utilizando:
myTextView.setText(Html.fromHtml(getString(R.string.html)));
Si tiene vínculos en su html y desea que se puedan hacer clic, utilice este método:
myTextView.setMovementMethod(LinkMovementMethod.getInstance());
Aquí está la mayoría de los ejemplos. No creo que la etiqueta pre
sea compatible.
Este es el archivo strings.xml
:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Formatting</string> <string name="link"><b>Hello World</b> This is a test of the URL <a href="http://www.example.com/">Example</a></string> <string name="bold"><b>This text is bold</b></string> <string name="emphasis"><em>This text is emphasized</em></string> <string name="sup">This is <sub>subscript</sub> and <sup>superscript</sup></string> </resources>
Aquí está el diseño. Tenga en cuenta que el enlace para que se pueda hacer clic en realidad, hay un poco de trabajo adicional necesario:
<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/test1" android:linksClickable="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="12dp" android:text="" android:textAppearance="?android:attr/textAppearanceMedium"/> <TextView android:id="@+id/test2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="12dp" android:text="" android:textAppearance="?android:attr/textAppearanceMedium"/> <TextView android:id="@+id/test3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="12dp" android:text="" android:textAppearance="?android:attr/textAppearanceMedium"/> <TextView android:id="@+id/test4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="12dp" android:text="" android:textAppearance="?android:attr/textAppearanceMedium"/> </LinearLayout> </ScrollView>
Finalmente, el código:
TextView test1 = (TextView)findViewById(R.id.test1); Spanned spanned = Html.fromHtml(getString(R.string.link)); test1.setMovementMethod(LinkMovementMethod.getInstance()); test1.setText(spanned); TextView test2 = (TextView)findViewById(R.id.test2); test2.setText(Html.fromHtml(getString(R.string.bold))); TextView test3 = (TextView)findViewById(R.id.test3); test3.setText(Html.fromHtml(getString(R.string.emphasis))); TextView test4 = (TextView)findViewById(R.id.test4); test4.setText(Html.fromHtml(getString(R.string.sup)));
String.xml puede contener entidades HTML, así:
<resources> <string name="hello_world"><span></string> </resources>
En su código: getResources().getString(R.string.hello_world);
Evaluará a "<span>"
. Puede utilizar este texto con formato HTML como este:
TextView helloWorld = (TextView)findViewById(R.id.hello_world); helloWorld.setText(Html.fromHtml(getString(R.string.hello_world)));
Todo el estilo soportado por el sistema de recursos XML se explica en la documentación de Android.
Recursos de cadenas: formato y estilo
Cualquier cosa incluida allí se puede utilizar y establecer directamente en TextView
. Si necesita usar más marcado HTML, tendrá que colocar HTML en bruto (con caracteres de escape para <
>
y otros) en el recurso y cargar todo en un WebView
.
Esto funcionó para mí:
<?xml version="1.0" encoding="utf-8"?>
<string name="app_name">Sangamner College</string> <string name="about_desc"><![CDATA[In order to make higher education available in the rural environment such as of Sangamner, Shikshan Prasarak Sanstha was established in 1960. Sangamner College was established by Shikshan Prasarak Sanstha, Sangamner on 23rd January 1961 on the auspicious occasion of Birth Anniversary of Netaji Subhashchandra Bose.The Arts and Commerce courses were commenced in June 1961 and in June 1965 Science courses were introduced. When Sangamner College was founded forty years ago, in 1961, there was no college available to the rural youth of this region. <br><br></>The college was founded with the aim of upliftment of the disadvantageous rural youth in all respects. On one hand, we are aware of the social circumstances prevailing in the rural area where we are working. So, we offer the elective option to students, which are favourable to the local atmosphere. On the other hand, we want to academically empower the aspiring youth by offering vocational course in Computer Applications to students of Arts & Commerce. BBA, BCA and MCA courses were started with the same purpose. “Think globally, act locally” is our guiding Principle.]]></string>
- Android: ¿Cómo puedes alinear un botón en la parte inferior y listview arriba?
- Es la palabra obsoleta la única diferencia entre fill_parent y match_parent