试图从在的strings.xml Android中设置的TextView图像 [英] Trying to set an image in textView from in Strings.xml in android

查看:95
本文介绍了试图从在的strings.xml Android中设置的TextView图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置存储在资产文件夹中的strings.xml的图像,并设置该字符串一个TextView。这可能吗?

I am trying to set an image stored in assets folder in strings.xml and set that string to a TextView. Is this possible?

strings.xml中有以下字符串:

Strings.xml has the following string:

<string name="nofriends">No Friends.. <![CDATA[<br><br><br><br> <img src=\"file:///android_asset/sad.png\"/>]]></string>

在我的java文件我设置这样的事情

And in my java file I am setting something like this"

Spanned text = Html.fromHtml(res.getString(R.string.nofriends));
noFriends.setText(text);

我得到一个绿色的小框中的文本,但不是实际的形象?

I am getting the text with a small green box but not the actual image?

任何人可以帮助我解决这个问题?

Can anybody help me fix this?

推荐答案

您可以使用ImageGetter从绘制资源获取的图像。

You can use ImageGetter to get image from drawable resource.

ImageGetter imageGetter = new ImageGetter() {

    @Override
    public Drawable getDrawable(String source) {
        Drawable d = getResources().getDrawable(R.drawable.yourimage);
        d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
        return d;
    }
};

Spanned spanned = Html.fromHtml(
    "<img src='" + getResources().getDrawable(R.drawable.yourimage) + "'/>", imageGetter, null);

现在将其设置为TextView的

now set it to TextView

textView.setText(spanned);

修改

<string name="nofriends">No Friends.. ####</string> //#### to replace with image

String s=res.getString(R.string.nofriends);
s=s.replace("####",spanned);
textView.setText(s);

这篇关于试图从在的strings.xml Android中设置的TextView图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆