android
  • html
  • textview
  • imageview
  • settext
  • 2013-05-06 70 views 5 likes 
    5

    我试图在Html.fromHtml()中显示文字和图像,但它在图像显示中不起作用。Html.fromHtml中的android-TextView setText显示图像和文字

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

    文本显示正常,但图像无法显示。如何改善呢?

    +0

    使用ImageGetter,例如看到这个http://stackoverflow.com/questions/16179285/html-imagegetter-textview/16209680#16209680 – pskink 2013-05-06 10:20:55

    +0

    我已经尝试...仍然不能得到它...: ( – user2274349 2013-05-06 10:34:21

    +0

    什么“你不明白”? – pskink 2013-05-06 10:58:59

    回答

    22

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

    这是我编辑......这些编码运行良好,对我......非常感谢那些帮助我...赞赏〜^^

    +0

    不错的答案!!!!!!!! ! – duggu 2014-01-13 06:22:16

    +1

    把tui.png图像放在哪里? – 2014-02-18 18:11:32

    +0

    把图像放在哪里??? – 2014-06-04 07:58:34

    相关问题