android-TextView setText in Html.fromHtml 显示图片和文字 [英] android-TextView setText in Html.fromHtml to display image and text

查看:13
本文介绍了android-TextView setText in Html.fromHtml 显示图片和文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I try to display the text and image in Html.fromHtml() but it is doesn't work in image display.

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>"));

文字显示良好,但图像无法显示.如何改进?

The text are display well but the image cannot display. How can improve it?

推荐答案

这是供用户参考的编码 pskink...

This is the coding for reference to user 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;
    }

}

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

This is what I edit... These coding run well for me... Thanks a lot for those helping me... Appreciated~ ^^

这篇关于android-TextView setText in Html.fromHtml 显示图片和文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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