歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android textview字體顏色顯示和圖片顯示

Android textview字體顏色顯示和圖片顯示

日期:2017/3/1 11:07:49   编辑:Linux編程

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


Copyright © Linux教程網 All Rights Reserved