简体   繁体   English

Android html.fromHtml无法正常工作

[英]Android html.fromHtml not working

I am trying to add some text and an image in a TextView using Html.fromHtml(), but it's not working. 我正在尝试使用Html.fromHtml()在TextView中添加一些文本和图像,但是它不起作用。

Getting the string from resources and calling Html.fromHtml(): 从资源获取字符串并调用Html.fromHtml():

String insertedText = "<img src=\"small_image\"/>";

String strText = getResources().getString(R.string.big_string, insertedText);

myTextView.setText(Html.fromHtml(strText, context, null));

Where context is an activity that also implements ImageGetter. 上下文是一项同时实现ImageGetter的活动。 This is the getDrawable function: 这是getDrawable函数:

public Drawable getDrawable(String source) {
    int id = 0;
    Drawable drawable = null;
    if(source.equals("small_image")){
        id = R.drawable.small_image;
    }
    if (id > 0) {
        drawable = getResources().getDrawable(id);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
    }
    return drawable;
}

In the strings.xml file in values, I have: 在values.strings.xml文件中,我有:

<string name="big_string">"Big String %1$s"</string>

I have small_image.png in the drawable folder and while debugging, I can see that id gets a correct value and then the bitmap is loaded in the drawable variable, but it still renders the rectangle with 'obj' written in it. 我在drawable文件夹中有small_image.png,在调试时,我可以看到id获得正确的值,然后将位图加载到drawable变量中,但它仍然渲染带有'obj'的矩形。

Any idea why it does that? 知道为什么这样做吗?

I also tried using SpannableStringBuilder and add an image span that would have replaced a 'space' from my text, but that didn't work either. 我还尝试使用SpannableStringBuilder并添加了一个图像范围,该范围将替换文本中的“空格”,但这也不起作用。

I have found the problem with my code. 我发现我的代码有问题。 Everything I wrote in the question is correct and you can use it as a reference for incorporating images in TextViews. 我在问题中写的所有内容都是正确的,您可以将其用作在TextViews中合并图像的参考。

The problem was that I was using the attribute : 问题是我正在使用属性:

android:textAllCaps="true"

In the xml file and that somehow prevented the image from being shown. 在xml文件中,以某种方式阻止了图像的显示。

  String name = modelDispatchReportList.get(position).getName();   //get name from model class typed List
    String text = "<font color='#000000'>" + name + "</font>";  //change color of name

    /*  check version of API to call method of Html class because fromHtml method is deprecated
    * */
    if (android.os.Build.VERSION.SDK_INT <= android.os.Build.VERSION_CODES.N) {
        Log.d(TAG, "onBindViewHolder: if");
        holder.textViewName.setText("12345" + " ");    //set text to textView
        holder.textViewName.append(Html.fromHtml(text));    //append text to textView
    } else {
        Log.d(TAG, "onBindViewHolder: else");
        holder.textViewName.setText("12345" + " ");    //set text to textView
        holder.textViewName.append(Html.fromHtml(text, 0)); ////append text to textView
    }

well, I tried your code, it' s working... 好吧,我尝试了您的代码,它正在工作...

btw, I used the deafult ic_lancher drawable... 顺便说一句,我用了聋哑的ic_lancher drawable ...

i just simply set the the textview to setContentView . 我只是将textview设置为setContentView

i guess is the textview' s layout or drawable' s problem? 我猜是textview的布局或drawable的问题吗?

could you tell me more? 你能告诉我更多吗?

只需将Textview更改为WebView并将内容设置为WebView

您不能通过html在textview上设置图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM