【问题标题】:Show image in textview using Html.fromhtml();使用 Html.fromhtml() 在 textview 中显示图像;
【发布时间】:2014-04-22 01:42:19
【问题描述】:

我有一个列表适配器来显示来自我的社交网络的内容,我需要在文本中放入 img 标签,我尝试使用 Html.fromhtml 并且它正在格式化文本,但它没有显示 img,而是显示为灰色正方形。

我怎样才能做到这一点?我读过有关 ImageGetter 的文章,但我仍然不太清楚。

谢谢

【问题讨论】:

    标签: android html android-image


    【解决方案1】:

    您可以使用Html img tagHtml.ImageGetterTextView 中绘制Image。但请确保您的图片在resource drawable 文件夹中可用

    这是一个示例,图片将从资源中加载。

    String htmlText = "Hai <img src=\"ic_launcher\"> Hello";
    
    textView.setText(Html.fromHtml(htmlText, new Html.ImageGetter() {
    
    @Override
    public Drawable getDrawable(String source) {
       int resourceId = getResources().getIdentifier(source, "drawable",getPackageName());
       Drawable drawable = getResources().getDrawable(resourceId);
       drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
                return drawable;
            }
        }, null));
    

    【讨论】:

    • 谢谢,我已经修改了代码以使用 url 图片
    • 如果 htmlText 包含多个图像,如何获取这些图像,其中图像存在于 SD 卡中
    • @Libin 当我们的图像来自 SD CARD/INTERNAL STOARGE 时,我们如何使用代码..请帮助我
    【解决方案2】:
     String mIsi = (String) bundle.get("isi");
            isi.setText(Html.fromHtml(mIsi, new Html.ImageGetter() {
                @Override
                public Drawable getDrawable(final String source) {
                    Picasso.get()
                            .load(source).into(imageView, new Callback() {
                        @Override
                        public void onSuccess() {
                            drawable = imageView.getDrawable();
    
                            drawable.setBounds(0, 0,
                                    drawable.getIntrinsicWidth(),
                                    drawable.getIntrinsicHeight());
                        }
    
                        @Override
                        public void onError(Exception e) {
                            Log.v("test pic","err"+e.getMessage());
                        }
                    });
    
                    imageView.setVisibility(View.GONE);
    
                    return drawable;
                }
            }, null));
    

    【讨论】:

    • 嗨,欢迎来到 Stack Overflow。请为您的代码添加一些解释。至少,解释一下它与上面接受的答案有何不同/更好/更相关。
    猜你喜欢
    • 2013-04-16
    • 2013-04-30
    • 1970-01-01
    • 1970-01-01
    • 2014-05-21
    • 1970-01-01
    • 2016-05-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多