Android textview字体颜色显示和图片显示


1,可以在布局文件中设置

  1. <EditText  
  2.        Android:id="@+id/editText1"  
  3.        android:textColor="#2BD54D"  
  4.        android:layout_width="match_parent"  
  5.        android:layout_height="wrap_content" >  
  6.    </EditText>  

2,在代码中显示html代码

  1. editText2.setText(Html.fromHtml(  "<font color=#E61A6B>红色代码</font> "+ "<i><font color=#1111EE>蓝色斜体代码</font></i>"+"<u><i><font color=#1111EE>蓝色斜体加粗体下划线代码</font></i></u>"));  

效果图

 
  1. package rw.textView;  
  2.   
  3. import android.R.integer;  
  4. import android.app.Activity;  
  5. import android.app.SearchManager.OnCancelListener;  
  6. import android.graphics.drawable.Drawable;  
  7. import android.os.Bundle;  
  8. import android.text.Html;  
  9. import android.text.Html.ImageGetter;  
  10. import android.text.Spannable;  
  11. import android.text.SpannableString;  
  12. import android.text.style.ImageSpan;  
  13. import android.view.View;  
  14. import android.view.View.OnClickListener;  
  15. import android.widget.EditText;  
  16. import android.widget.ImageButton;  
  17.   
  18. public class TextViewTestActivity extends Activity {  
  19.     /** Called when the activity is first created. */  
  20.     private EditText editText,editText2,editText3;  
  21.     private ImageButton imageButton01,imageButton02,imageButton03;  
  22.     @Override  
  23.     public void onCreate(Bundle savedInstanceState) {  
  24.         super.onCreate(savedInstanceState);  
  25.         setContentView(R.layout.main);  
  26.         editText=(EditText) findViewById(R.id.editText1);  
  27.         editText2=(EditText) findViewById(R.id.editText2);  
  28.         editText3=(EditText) findViewById(R.id.editText3);  
  29.         imageButton01=(ImageButton) findViewById(R.id.imageButton1);  
  30.         imageButton02=(ImageButton) findViewById(R.id.imageButton2);  
  31.         imageButton03=(ImageButton) findViewById(R.id.imageButton3);  
  32.         editText2.setText(Html.fromHtml(  "<font color=#E61A6B>红色代码</font> ""<i><font color=#1111EE>蓝色斜体代码</font></i>"+"<u><i><font color=#1111EE>蓝色斜体加粗体下划线代码</font></i></u>"));  
  33.        // editText3.setText(Html.fromHtml("<img src='"+R.drawable.qq+"'/>", imageGetter,null));     
  34.         imageButton01.setOnClickListener(new MyListener());  
  35.         imageButton02.setOnClickListener(new MyListener());  
  36.         imageButton03.setOnClickListener(new MyListener());  
  37.     }  
  38.         class MyListener implements OnClickListener{  
  39.   
  40.             @Override  
  41.             public void onClick(View v) {  
  42.                 // TODO Auto-generated method stub   
  43.                 switch (v.getId()) {  
  44.                 case R.id.imageButton1:  
  45.                     SetImage(R.drawable.amazed);  
  46.                     break;  
  47.                 case R.id.imageButton2:  
  48.                     SetImage(R.drawable.angry);  
  49.                     break;  
  50.                 case R.id.imageButton3:  
  51.                     SetImage(R.drawable.isync);  
  52.                 break;  
  53.                 default:  
  54.                     break;  
  55.                 }  
  56.             }         
  57.         }  
  58.         void SetImage(int dra)  
  59.         {  
  60.             Drawable drawable=getResources().getDrawable(dra);  
  61.             drawable.setBounds(00, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());  
  62.             SpannableString spannableString = new SpannableString("pics");  
  63.             ImageSpan imageSpan=new ImageSpan(drawable,ImageSpan.ALIGN_BASELINE);  
  64.             spannableString.setSpan(imageSpan, 0, spannableString.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
  65.             editText3.setText(spannableString);             
  66.         }  
  67. }  


 

相关内容