伙计们,我有一个显示 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/