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

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


效果图

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值