web-dev-qa-db-fra.com

Android: html dans strings.xml

Je voudrais afficher par exemple ce code 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>

Je veux l'afficher sur un dialogue en déclarant HTML dans les ressources strings.xml. Comment puis-je le faire?

84
Alberto Alberto

Le meilleur moyen d’ajouter du code source HTML dans strings.xml est d’utiliser <![CDATA[html source code]]>. Voici un exemple:

<string name="html"><![CDATA[<p>Text</p>]]></string> 

Ensuite, vous pouvez afficher ce code HTML dans TextView en utilisant:

myTextView.setText(Html.fromHtml(getString(R.string.html)));

Si vous avez des liens dans votre code HTML et que vous voulez qu'ils soient cliquables, utilisez cette méthode:

myTextView.setMovementMethod(LinkMovementMethod.getInstance());
201
Mario Kutlev

Voici la plupart des exemples. Je ne pense pas que la balise pre soit supportée.

enter image description here

C'est le strings.xml fichier:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Formatting</string>
    <string name="link">&lt;b&gt;Hello World&lt;/b&gt; This is a test of the URL &lt;a href="http://www.example.com/"&gt;Example&lt;/a&gt;</string>
    <string name="bold">&lt;b&gt;This text is bold&lt;/b&gt;</string>
    <string name="emphasis">&lt;em&gt;This text is emphasized&lt;/em&gt;</string>
    <string name="sup">This is &lt;sub&gt;subscript&lt;/sub&gt; and &lt;sup&gt;superscript&lt;/sup&gt;</string>
</resources>

Voici la mise en page. Remarque: pour que le lien soit réellement cliquable, un peu de travail supplémentaire est nécessaire:

<?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>

Enfin, le code:

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)));
26
wsanville

String.xml peut contenir des entités HTML, comme ceci:

<resources>
    <string name="hello_world">&lt;span&gt;</string>
</resources>

Dans votre code: getResources().getString(R.string.hello_world); sera évalué à "<span>". Vous pouvez utiliser ce texte au format HTML comme ceci:

TextView helloWorld = (TextView)findViewById(R.id.hello_world);
helloWorld.setText(Html.fromHtml(getString(R.string.hello_world)));
5
Thomas

Tous les styles pris en charge par le système de ressources XML sont expliqués dans la documentation Android.

Ressources de chaîne: Mise en forme et style

Tout ce qui est inclus ici peut être utilisé et défini directement sur TextView. Si vous devez utiliser un balisage HTML supplémentaire, vous devez placer du code HTML brut (avec des caractères d'échappement pour &lt;, &gt; et autres) dans la ressource et charge le tout dans un WebView.

3
Devunwired

Cela a fonctionné pour moi:

<?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 &amp; Commerce. B.B.A., B.C.A. and M.C.A. courses were started with the same purpose. “Think globally, act locally” is our guiding Principle.]]></string>
2
Suyog Gunjal