4

是否有可能将 html 从 res/raw 加载到 TextView 中?我知道我可以使用 WebView,但该死的透明度并不总是有效(不是在每个设备上)

4

3 回答 3

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

//此函数将返回您可以在文本视图中设置的字符串。并且该字符串具有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();
  }
于 2013-08-24T18:12:13.737 回答
4

您可以将您的 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>
于 2013-08-24T18:08:01.673 回答
0

对于简单的 HTML 内容,您可以使用 .setText(Html.fromHtml(" Hello world!"))。只需要将html 文件从 raw 加载到字符串中

于 2013-08-24T18:08:31.640 回答