android - 如何使用textview在html文本中显示图像

标签 android textview

伙计们,我有一个显示 html 文本的 TextView ,它里面有图像标签。 我想使用 imageGetter 显示图像,但它只显示一个小蓝框。 几天来我一直在处理这个问题。所以请帮我解决这个问题。

我现在的代码是这样的

  TextView textD = (TextView) findViewById(R.id.body);
  textD.setText(Html.fromHtml(Body, imgGetter, null));
  textD.setMovementMethod(new ScrollingMovementMethod());

 private ImageGetter imgGetter = new ImageGetter() {

        public Drawable getDrawable(String source) {


          try{
           InputStream is = (InputStream) new URL(source).getContent();
           Drawable d = Drawable.createFromStream(is, "src name");
           return d;
          }catch (Exception e){
           Log.d("image", imageSource, e);
           return null;
          }

        }
};

最佳答案

引用资料说:

The setBounds(Rect) method must be called to tell the Drawable where it is drawn and how large it should be. All Drawables should respect the requested size, often simply by scaling their imagery. A client can find the preferred size for some Drawables with the getIntrinsicHeight() and getIntrinsicWidth() methods.

http://developer.android.com/reference/android/graphics/drawable/Drawable.html

那么试试这个:

d.setBounds(0,0,d.getIntrinsicWidth(),d.getIntrinsicHeight());

它会起作用的!

关于android - 如何使用textview在html文本中显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4552501/

相关文章:

安卓 : prevent an app from opening its own links

android - 将数据从一个 fragment 发送到另一个 fragment

android - Gradle 项目同步失败。 Osmdroid

android - Android 版本 < 6.0 中的文本椭圆大小行为

java - 处理长字符串和 TextView 行为

android - 通过 sql 循环将多个 TextView 动态添加到 TableRow

android - WifiManager enableNetwork 几秒钟后恢复到以前的网络

Android 动画时删除 View 错误

html - 从 XML 中的 html 格式的字符串资源设置 TextView 文本

java - Android:可点击 TextView 中的 ClickableSpan