【问题标题】:Is it possible to load a HTML file into a TextView in Android?是否可以将 HTML 文件加载到 Android 中的 TextView 中?
【发布时间】:2012-09-20 20:29:07
【问题描述】:

在我的应用程序中,我将一个 html 文档(包含:显示方程的代码、对库的引用)加载到 webview 中。

问题是加载仍然需要一些时间,而且感觉不是很流畅。我已经连接了一个进度条,让它更活泼一些。

我只是想知道,是否可以将该 html 文件加载到 textview 中?或者我可以将该html代码嵌入到文本视图中吗?

【问题讨论】:

标签: android html webview textview


【解决方案1】:

是的,这是可能的:

textView.setText(Html.fromHtml(myHTMLString));

【讨论】:

  • 我知道。您将如何在其中添加库和 css 的引用?
  • TextView 不呈现完整的 HTML。 HTML.fromHTML() 方法仅将几个 HTML 标签转换为 Textview 跨度。如果您需要包括 CSS 在内的完整 HMTL 支持,您应该使用 WebView
【解决方案2】:

是的,有可能实现这一点,您需要从本地存储文件路径转换输入流并创建一个字符串生成器来读取输入流中的每一行 -

    // "string" is a your html file path
    File file = new File(string);
    try {
        FileInputStream fileInputStream = new FileInputStream(file);
        BufferedReader r = new BufferedReader(new 
        InputStreamReader(fileInputStream));
        StringBuilder strBuilder = new StringBuilder();
        String line;
        while ((line = r.readLine()) != null) {
            strBuilder.append(line).append("\n");
        }
        //"total" is your converted html you can show this on textview
        areTextView.fromHtml(String.valueOf(strBuilder));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

【讨论】:

    猜你喜欢
    • 2014-05-13
    • 1970-01-01
    • 2011-08-30
    • 2020-09-26
    • 2015-07-27
    • 1970-01-01
    • 2021-12-11
    • 1970-01-01
    • 2019-02-02
    相关资源
    最近更新 更多