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);
- }
- }
相关推荐
IT之家 2020-03-11
graseed 2020-10-28
zbkyumlei 2020-10-12
SXIAOYI 2020-09-16
jinhao 2020-09-07
impress 2020-08-26
liuqipao 2020-07-07
淡风wisdon大大 2020-06-06
yoohsummer 2020-06-01
chenjia00 2020-05-29
baike 2020-05-19
扭来不叫牛奶 2020-05-08
hxmilyy 2020-05-11
黎豆子 2020-05-07
xiongweiwei00 2020-04-29
Cypress 2020-04-25
冰蝶 2020-04-20