1. 引言

在Android中,使某个字符串中的某个单词或汉字高亮,效果图及代码实现如下。

2. 效果图

3. 功能实现

1. 主界面(main.xml)实现:<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:orientation = "vertical" android:layout_width = "fill_parent" android:layout_height = "fill_parent" > <TextView android:id = "@+id/highLight" android:layout_width = "wrap_content" android:layout_height = "wrap_content" /> </LinearLayout>

2. 主Activity实现: package com.focus.fishme; 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.widget.TextView; public class HighLightActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TextView highLightView = (TextView) findViewById(R.id.highLight); String highLightStr = "HighLight MaYingCai"; String highLight = "MaYingCai"; int start = highLightStr.indexOf(highLight); SpannableStringBuilder style = new SpannableStringBuilder(highLightStr); style.setSpan(new BackgroundColorSpan(Color.RED), start, start + highLight.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); highLightView.setText(style); } }

更多相关文章

  1. Android实现九宫格图案解锁
  2. Android(安卓)代码执行Linux Shell小记
  3. android 横向滚动屏幕实现(1)
  4. Android(安卓)开源项目分类汇总
  5. Android(安卓)实现跑马灯效果
  6. Android实现发送短信功能实例详解
  7. android全屏去掉title栏的多种实现方法
  8. 在Android中实现文件读写
  9. 浅谈Java中Collections.sort对List排序的两种方法

随机推荐

  1. Android中RecyclerView Adapter的骚操作
  2. 睡眠唤醒机制简介
  3. 《Android和PHP最佳实践》官方站
  4. Android: 打印Bundle内容
  5. Android崩溃后重启
  6. onAttachToWindow() 调用
  7. android实现电话状态监控
  8. Android(安卓)Bundle类
  9. Android(安卓)Gradle Study
  10. Android(安卓)AsyncTask 源码分析详解