2012-11-16 10 views
68

内のhtml:アンドロイド:私は、たとえば、このhtmlコード表示を希望のstrings.xml

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

を私は資源strings.xmlでHTMLを宣言することによって、ダイアログに表示します。どうしたらいいですか?あなたのコードで

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

+0

チェックアウト:[XMLでのHTML形式の文字列リソースからの設定のTextViewのテキスト](http://stackoverflow.com/questions/3235131/set-textview-text-from-html- formatted-string-resource-in-xml) – Sam

答えて

3

string.xmlをすることができますので同じように、HTMLエンティティが含まれているgetResources().getString(R.string.hello_world);"<span>"と評価されます。このようなHTML形式のテキストを次のように使用することができます。

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

XMLリソースシステムでサポートされているすべてのスタイリングについては、Androidのマニュアルで説明しています。

String Resources: Formatting and Styling

何が使用され、TextViewに直接設定することができる含ま。さらにHTMLマークアップを使用する必要がある場合は、生のHTML(エスケープ文字を&lt;&gt;など)をリソースに配置し、全体をWebViewにロードする必要があります。

18

ほとんどの例があります。私はpreタグがサポートされているとは思わない。

enter image description here

これはstrings.xmlファイルです:

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

ここレイアウトです。

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

最後に、コード:リンクが実際にクリック可能であるために注意し、必要な余分な作業のビットがあります

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))); 
+0

ありがとう..これはうまく動作します –

+0

神様にはありがとうございます<と> 非常にうまく動作します。 –

161

のstrings.xmlがにあるのHTMLソースコードを追加するための最良の方法<![CDATA[html source code]]>を使用してください。ここでは一例です:あなたは、HTML内のリンクを持っていて、それらをクリッカブルになりたい場合は、

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

をこのメソッドを使用します:

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

次に、あなたが使ってTextViewの中で、このHTMLを表示することができます

myTextView.setMovementMethod(LinkMovementMethod.getInstance()); 
+4

'getString()'の代わりに 'getText()'を使うと、HTML _without_CDATAを使うことができます:http://stackoverflow.com/a/18199543/89818 – caw

+13

はい、しかし実際には 'CDATA'はるかに簡単です - <, >などをすべて翻訳する必要はありません。実際のHTMLをコピーしてstrings.xmlに貼り付けてください。 –

+1

受け入れられる回答である必要があります。 Thx –

0

これが私の仕事:

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

関連する問題