在Android小部件上使用HTML和TextView显示图片

3

我正在尝试在主屏幕小部件上动态显示图片。由于RemoveViews提供了非常有限的方法来与布局交互,因此我尝试使用HTML来达到相同的效果。

普通的HTML可以正常显示(如<u><b>等),但是我无法显示图片。相反,我在TextView中看到一个小的空白方块。

例如:

s = "<b>Hello</b> <img src='world'/>";
ImageGetter imgGetter = new ImageGetter(context);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
views.setTextViewText(R.id.textarea, Html.fromHtml(s, imgGetter, null));

图像获取器看起来像下面这样:
public class ImageGetter implements Html.ImageGetter
{
    Context c;

    public ImageGetter(Context c)
    {
        this.c = c;
    }
    public Drawable getDrawable(String source) {
        Drawable d = null;
        int resID = c.getResources().getIdentifier(source, "drawable", c.getPackageName());

        d = c.getApplicationContext().getResources().getDrawable(resID);
        d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());

        return d;
    }
}

如果我使用常规的Activity,代码可以很好地运行。我怀疑这是在使用AppWidgetProvider时与Android一起使用的一个错误/未记录特性。我只想得到确认。


我可以确认。当在RemoteViews中使用时,它不会显示图像。 - waqaslam
1个回答

0
我正在尝试在主屏幕小部件上动态显示图片。 请使用ImageView。当您希望更改图像时,请更新应用程序小部件。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接