2013-08-24 42 views

回答

7
myTextView.setText(Html.fromHtml(readTxt()));  

//此函數將返回可以在您的textview中設置的字符串。並且該字符串有html代碼,所以使用Html.fromHtml

private String readTxt() { 
    InputStream inputStream = getResources().openRawResource(R.raw.My_html_file); 
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 
    int i; 
    try { 
     i = inputStream.read(); 
     while (i != -1) { 
      byteArrayOutputStream.write(i); 
      i = inputStream.read(); 
     } 
     inputStream.close(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    return byteArrayOutputStream.toString(); 
    } 
+0

非常感謝!結果令我震驚:)它肯定看起來不像它看起來在html – user2234594

+0

你得到不正確的數據? – Rahul

+0

@ user2234594您的問題已解決? – Rahul

3

你可以把你的HTML內容的string資源,並通過使用它在你的TextView:

textView.setText(Html.fromHtml(getResource().getString(R.string.my_html))); 

要格式化的strings.xml文件的HTML,使用語法:

<string name="my_html"> 
    <![CDATA[ 
    Your html content here 
    ]]> 
</string> 
相關問題