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

fmms 12年前發布 | 96K 次閱讀 Android Android開發 移動開發

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

 <EditText
        android:id="@+id/editText1"
        android:textColor="#2BD54D"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </EditText>
2,在代碼中顯示html代碼
editText2.setText(Html.fromHtml(  "<font color=#E61A6B>紅色代碼</font> "+ "<i><font color=#1111EE>藍色斜體代碼</font></i>"+"<u><i><font color=#1111EE>藍色斜體加粗體下劃線代碼</font></i></u>"));

效果圖

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

package rw.textView;

import android.R.integer;
import android.app.Activity;
import android.app.SearchManager.OnCancelListener;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Html;
import android.text.Html.ImageGetter;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ImageSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;

public class TextViewTestActivity extends Activity {
    /** Called when the activity is first created. */
    private EditText editText,editText2,editText3;
    private ImageButton imageButton01,imageButton02,imageButton03;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        editText=(EditText) findViewById(R.id.editText1);
        editText2=(EditText) findViewById(R.id.editText2);
        editText3=(EditText) findViewById(R.id.editText3);
        imageButton01=(ImageButton) findViewById(R.id.imageButton1);
        imageButton02=(ImageButton) findViewById(R.id.imageButton2);
        imageButton03=(ImageButton) findViewById(R.id.imageButton3);
        editText2.setText(Html.fromHtml(  "<font color=#E61A6B>紅色代碼</font> "+ "<i><font color=#1111EE>藍色斜體代碼</font></i>"+"<u><i><font color=#1111EE>藍色斜體加粗體下劃線代碼</font></i></u>"));
       // editText3.setText(Html.fromHtml("<img src='"+R.drawable.qq+"'/>", imageGetter,null));  
        imageButton01.setOnClickListener(new MyListener());
        imageButton02.setOnClickListener(new MyListener());
        imageButton03.setOnClickListener(new MyListener());
    }
        class MyListener implements OnClickListener{

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                switch (v.getId()) {
                case R.id.imageButton1:
                    SetImage(R.drawable.amazed);
                    break;
                case R.id.imageButton2:
                    SetImage(R.drawable.angry);
                    break;
                case R.id.imageButton3:
                    SetImage(R.drawable.isync);
                break;
                default:
                    break;
                }
            }       
        }
        void SetImage(int dra)
        {
            Drawable drawable=getResources().getDrawable(dra);
            drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
            SpannableString spannableString = new SpannableString("pics");
            ImageSpan imageSpan=new ImageSpan(drawable,ImageSpan.ALIGN_BASELINE);
            spannableString.setSpan(imageSpan, 0, spannableString.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            editText3.setText(spannableString);           
        }
}

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

 

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

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

 本文由用戶 fmms 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!