有沒有可能從res/raw加載html到TextView? 我知道我可以使用的WebView,但該死的透明度並不總是工作(不是每個設備上)java android-如何將資源設置爲TextView?
3
A
回答
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();
}
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>
0
對於簡單的HTML內容,您可以使用.setText(Html.fromHtml( 「你好世界!」))。只需要load html file to string from raw。
相關問題
- 1. Android-如何將亮度設置爲textView?
- 2. 如何將android textview設置爲Integer
- 3. 的Android的Java設置圖像資源
- 4. Android - 將高度設置爲TextView動態
- 5. 將TextView設置爲TimePicker的時間 - Android
- 6. 將Android TableView和TextView作爲單元格 - 如何將TextView設置爲多行
- 7. 如何爲android textview設置'linebreakmode'(FormattedText)
- 8. Android - 如何設置ArrayList的值爲TextView
- 9. 將TextView設置爲TableRow,IllegalStateException - Android
- 10. Android - 將文本設置爲TextView
- 11. 如何將String數組設置爲TextView?
- 12. 如何將TextView設置爲Long Value?
- 13. 將unique_ptr資源設置爲nullptr後,它將如何釋放?
- 14. 將ImageView資源設置爲Drawable
- 15. 將原始資源設置爲鈴聲
- 16. 如何爲Android資源設置固定ID
- 17. Android:如何爲Spinner設置DropDown查看資源?
- 18. Android:如何使用textview.setText(getter)將TextView設置爲getter方法
- 19. 如何將背景圖像設置爲android中的textview?
- 20. 如何將TextView設置爲onClick的情況? Android的
- 21. 如何在android中將複選框的值設置爲textview?
- 22. 將文本設置爲textView
- 23. 如何將標籤設置爲一個資源與耶拿
- 24. 如何將TextBlock文本屬性設置爲字符串資源?
- 25. 如何將資源文件設置爲非嵌入式
- 26. 如何使用資源包設置fx:value?
- 27. 爲什麼我不能將文本設置爲Android TextView?
- 28. 如何設置令牌的資源ID?
- 29. Android TextView未設置
- 30. android中設置ImageView資源出錯
非常感謝!結果令我震驚:)它肯定看起來不像它看起來在html – user2234594
你得到不正確的數據? – Rahul
@ user2234594您的問題已解決? – Rahul