[英]java android - how to set html from resources to TextView?
Is there any possibility to load html from res/raw into the TextView? 是否有可能将res / raw中的html加载到TextView中? I know I can use WebView, but damn transparency is not always working (not on every device)
我知道我可以使用WebView,但该死的透明度并不总是有效(不是在每台设备上)
myTextView.setText(Html.fromHtml(readTxt()));
//This function will return string which you can set in your textview. //此函数将返回您可以在textview中设置的字符串。 And that String have html codes so use Html.fromHtml
并且该String具有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();
}
You can put your html content in a string
resource and use it in your TextView by : 您可以将html内容放在
string
资源中,并通过以下方式在TextView中使用它:
textView.setText(Html.fromHtml(getResource().getString(R.string.my_html)));
To format your HTML in strings.xml file, use that syntax : 要在strings.xml文件中格式化HTML,请使用以下语法:
<string name="my_html">
<![CDATA[
Your html content here
]]>
</string>
For simple HTML content you can use .setText(Html.fromHtml(" Hello world!")).
对于简单的HTML内容,您可以使用.setText(Html.fromHtml(“ Hello world!”))。 Just need to load html file to string from raw .
只需要从原始加载html文件到字符串 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.