android-TextView setText in Html.fromHtml 显示图片和文字

标签 android html textview imageview settext

<分区>

我尝试在 Html.fromHtml() 中显示文本和图像,但它在图像显示中不起作用。

message = (TextView) findViewById (R.id.message);  

message.setText(Html.fromHtml(
        "<p><b>First, </b><br/>" +
        "Please press the" + "<img src = 'addbutton.png' />" + " to insert a new event.</p>"));

文字显示正常,图片显示不出来。如何改进?

最佳答案

这是供用户引用的编码pskink ...

    package com.tutorial.myjob;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LevelListDrawable;
import android.os.Bundle;
import android.text.*;
import android.text.Html.ImageGetter;
import android.widget.*;

public class HelpMenu extends Activity implements ImageGetter{

    TextView message;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.help_menu);
        String code = "<p><b>First, </b><br/>" +
                "Please press the <img src ='addbutton.png'> button beside the to insert a new event.</p>" +
                "<p><b>Second,</b><br/>" +
                "Please insert the details of the event.</p>"
                "<p>The icon of the is show the level of the event.<br/>" +
                "eg: <img src = 'tu1.png' > is easier to do.</p></td>";

        message = (TextView) findViewById (R.id.message);       
        Spanned spanned = Html.fromHtml(code, this, null);
        message.setText(spanned);
        message.setTextSize(16);


    }

    @Override
    public Drawable getDrawable(String arg0) {
        // TODO Auto-generated method stub
        int id = 0;

        if(arg0.equals("addbutton.png")){
            id = R.drawable.addbutton;
        }

        if(arg0.equals("tu1.png")){
            id = R.drawable.tu1;
        }
        LevelListDrawable d = new LevelListDrawable();
        Drawable empty = getResources().getDrawable(id);
        d.addLevel(0, 0, empty);
        d.setBounds(0, 0, empty.getIntrinsicWidth(), empty.getIntrinsicHeight());

        return d;
    }

}

这是我编辑的...这些代码对我来说运行良好...非常感谢那些帮助我的人...感谢~ ^^

关于android-TextView setText in Html.fromHtml 显示图片和文字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16396462/

相关文章:

java - 带数据包的套接字?我应该用什么?

html - 返回页首 带有缓慢动画的链接(仅限 CSS)

java - 使 TextView 的按钮更改值

android - 如果 textview 到达我包含的布局 Android 中的 View ,它应该滚动

android - SQLiteException 语法错误不清楚

android - QCAR中使用的图像检测技术

javascript - 根据内容溢出更改表格单元格内容

android - 在 MyAdapter.getView 获取 Nullpointer,如何解决?

android - iconifiedByDefault 的 xml 版本被忽略

php - 使用一个 SQL 表中的数据并将其与另一表中的数据关联