转载自:http://bbs.9ria.com/thread-234291-1-1.html
设置部分文字背景高亮显示代码:
- 复制到剪贴板 Java代码
- public class HighLightActivity extends Activity {
- String strs="我的心太乱了,给我点空白。";
- TextView textview;
- int start =3;
- int end = 5;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.high_light);
- textview=(TextView)findViewById(R.id.textview);
- SpannableStringBuilder style=new SpannableStringBuilder(strs);
- style.setSpan(new BackgroundColorSpan(Color.RED),start,end,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
- textview.setText(style);
-
- }
-
- }
-
-
-
-
- 同时设置文字和背景高亮显示:
- 复制到剪贴板 Java代码
- package com.justel.contact;
-
- import android.app.Activity;
- import android.graphics.Color;
- import android.os.Bundle;
- import android.text.Spannable;
- import android.text.SpannableStringBuilder;
- import android.text.style.BackgroundColorSpan;
- import android.text.style.ForegroundColorSpan;
- import android.widget.TextView;
-
- public class HighLightActivity extends Activity {
- String strs="我的心太乱了,给我点空白。";
- TextView textview;
- int start =3;
- int end = 5;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.high_light);
- textview=(TextView)findViewById(R.id.textview);
- SpannableStringBuilder style=new SpannableStringBuilder(strs);
- style.setSpan(new BackgroundColorSpan(Color.RED),start,end,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
- style.setSpan(new ForegroundColorSpan(Color.RED),7,9,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
- textview.setText(style);
-
- }
-
- }
原文连接: http://dev.10086.cn/cmdn/wiki/index.php?doc-view-5772.html