【问题标题】:Images in android textview using fromHtml method doesn't show up使用fromHtml方法的android textview中的图像不显示
【发布时间】:2012-08-25 23:23:23
【问题描述】:

我正在尝试使用 Html.fromHtml 方法在我的帮助文件中显示图像。

这是我的java代码

            TextView tv = (TextView)findViewById(R.id.text);
        tv.setText(Html.fromHtml(getString(R.string.help), new Html.ImageGetter() {
            @Override
             public Drawable getDrawable(String source) {
                    int id;

                    if (source.equals("top_scrollers.png")) {
                        id = R.drawable.top_scrollers;
                    }
                    else if (source.equals("prices.png")) {
                        id = R.drawable.prices;
                    }
                    else if (source.equals("percentage.png")) {
                        id = R.drawable.percentage;
                    }
                    else if (source.equals("mplus.png")) {
                        id = R.drawable.mplus;
                    }
                    else {
                        return null;
                    }

                    Drawable d = getResources().getDrawable(id);
                    d.setBounds(0,0,d.getIntrinsicWidth(),d.getIntrinsicHeight());
                    return d;
                }
        }, null));

这是我的xml

<string name="help">
<![CDATA[
<h1>Help</h1>
<p>
<h3>Getting stone price</h3>
<img src="top_scrollers.png"/>
</p>
]]>
</string>

如您所见,我将图像放置在 cdata 文本中,并且我创建了一种从它们创建可绘制对象的方法,但它只是不可显示。 有什么建议吗?

【问题讨论】:

  • 你应该为此尝试 webview。
  • 只需在 webview 中加载 htmlform 数据,例如 webview.loadData(Html.fromHtml(getString(R.string.help), "text/html", null);

标签: android html image


【解决方案1】:

您可以使用 WebView 代替 TextView 。试试这个解决方案:

1) 将您的 top_scrollers.png 放入 assets 文件夹

2) 在 raw 文件夹中创建 your_html.html,内容为:

<h1>Help</h1>
<p>
<h3>Getting stone price</h3>
<img src="file:///android_asset/top_scrollers.png"/>
</p> 

3) 要将此原始文件作为字符串读取,您可以阅读此内容 Android read text raw resource file

4) 之后,您可以在 webView 中加载此文本:

webView.loadDataWithBaseURL("", your_raw_string, "text/html", "utf-8", "");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-16
    • 1970-01-01
    • 1970-01-01
    • 2015-05-26
    • 1970-01-01
    • 2014-11-25
    • 1970-01-01
    • 2016-09-22
    相关资源
    最近更新 更多