2012-11-16 3 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 형식의 문자열 리소스에서 설정 텍스트 뷰의 텍스트 (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 마크 업을 사용해야하는 경우 &lt;, &gt; 등과 같은 이스케이프 문자가 포함 된 원시 HTML을 리소스에 배치하고 전체 내용을 WebView에로드해야합니다.

18

다음은 대부분의 예입니다. pre 태그가 지원되지 않는다고 생각합니다.

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

여기 레이아웃입니다 :

enter image description here

이는 strings.xml 파일입니다.

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

그런 다음 당신이 사용하는 텍스트 뷰에서이 HTML을 표시 할 수 있습니다

다음은 예입니다
myTextView.setMovementMethod(LinkMovementMethod.getInstance()); 
+4

'getString()'대신'getText()'를 사용하면 HTML _without_ CDATA를 사용할 수 있습니다 : http://stackoverflow.com/a/18199543/89818 – caw

+13

예, 포함하는 실제 HTML은'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>