Html.fromHtml中的android-TextView setText显示图像和文本[复制]

前端之家收集整理的这篇文章主要介绍了Html.fromHtml中的android-TextView setText显示图像和文本[复制]前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
参见英文答案 > Html.ImageGetter TextView3个
我尝试在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,empty);
        d.setBounds(0,empty.getIntrinsicWidth(),empty.getIntrinsicHeight());

        return d;
    }

}

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

原文链接:https://www.f2er.com/html/225433.html

猜你在找的HTML相关文章