【问题标题】:android-TextView setText in Html.fromHtml to display image and text [duplicate]android-TextView setText在Html.fromHtml中显示图像和文本[重复]
【发布时间】:2013-04-30 01:46:04
【问题描述】:

我尝试在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>"));

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

【问题讨论】:

标签: android html textview imageview settext


【解决方案1】:

这是参考用户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;
    }

}

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

【讨论】:

  • 在哪里放置 tui.png 图片?
  • 把图片放在哪里???
  • 对于那些不确定将图像放在哪里的人。被覆盖的getDrawable() 方法接受String 参数,它是html 中的src=(即tu1.png) .然后该方法解析传入的String 并分配要返回的drawable(即R.drawable.tu1)。图像(或可绘制)应放置在 /res/drawable-xhdpi/ 文件夹(或任何/所有其他 /res/drawable-xxxxxx/ 文件夹)中。
  • 如何使用此解决方案使图像内联到 textview?
猜你喜欢
  • 2013-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多