生活随笔
收集整理的這篇文章主要介紹了
Android textview字体颜色显示和图片显示
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1,可以在布局文件中設置
| 2 | ???????android:id="@+id/editText1" |
| 3 | ???????android:textColor="#2BD54D" |
| 4 | ???????android:layout_width="match_parent" |
| 5 | ???????android:layout_height="wrap_content" > |
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>")); |
效果圖
| 03 | import android.R.integer; |
| 04 | import android.app.Activity; |
| 05 | import android.app.SearchManager.OnCancelListener; |
| 06 | import android.graphics.drawable.Drawable; |
| 07 | import android.os.Bundle; |
| 08 | import android.text.Html; |
| 09 | 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; |
| 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; |
| 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()); |
| 38 | ????????class MyListener implements OnClickListener{ |
| 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); |
| 53 | ????????????????default: |
| 54 | ????????????????????break; |
| 58 | ????????void SetImage(int dra) |
| 60 | ????????????Drawable drawable=getResources().getDrawable(dra); |
| 61 | ????????????drawable.setBounds(0, 0, 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);??????????? |
?
總結
以上是生活随笔為你收集整理的Android textview字体颜色显示和图片显示的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。