android--设置TextView部分文字的颜色和背景(高亮显示)



转载自:http://bbs.9ria.com/thread-234291-1-1.html


设置部分文字背景高亮显示代码:

  1. 复制到剪贴板  Java代码
  2. public class HighLightActivity extends Activity {      
  3.     String strs="我的心太乱了,给我点空白。";      
  4.     TextView textview;      
  5.     int start =3;      
  6.     int end = 5;      
  7.     /** Called when the activity is first created. */     
  8.     @Override     
  9.     public void onCreate(Bundle savedInstanceState) {      
  10.         super.onCreate(savedInstanceState);      
  11.         setContentView(R.layout.high_light);      
  12.         textview=(TextView)findViewById(R.id.textview);      
  13.         SpannableStringBuilder style=new SpannableStringBuilder(strs);      
  14.         style.setSpan(new BackgroundColorSpan(Color.RED),start,end,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);      
  15.         textview.setText(style);      
  16.               
  17.     }      
  18.      
  19. }     
  20.   
  21.   
  22.   

  23. 同时设置文字和背景高亮显示: 
  24. 复制到剪贴板  Java代码
  25. package com.justel.contact;      
  26.      
  27. import android.app.Activity;      
  28. import android.graphics.Color;      
  29. import android.os.Bundle;      
  30. import android.text.Spannable;      
  31. import android.text.SpannableStringBuilder;      
  32. import android.text.style.BackgroundColorSpan;      
  33. import android.text.style.ForegroundColorSpan;      
  34. import android.widget.TextView;      
  35.      
  36. public class HighLightActivity extends Activity {      
  37.     String strs="我的心太乱了,给我点空白。";      
  38.     TextView textview;      
  39.     int start =3;      
  40.     int end = 5;      
  41.     /** Called when the activity is first created. */     
  42.     @Override     
  43.     public void onCreate(Bundle savedInstanceState) {      
  44.         super.onCreate(savedInstanceState);      
  45.         setContentView(R.layout.high_light);      
  46.         textview=(TextView)findViewById(R.id.textview);      
  47.         SpannableStringBuilder style=new SpannableStringBuilder(strs);      
  48.         style.setSpan(new BackgroundColorSpan(Color.RED),start,end,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);      
  49.         style.setSpan(new ForegroundColorSpan(Color.RED),7,9,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);      
  50.         textview.setText(style);      
  51.               
  52.     }      
  53.      
  54. }   
复制代码

原文连接: http://dev.10086.cn/cmdn/wiki/index.php?doc-view-5772.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值