1. 引言

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

2. 效果图

Android中字符串片段高亮_第1张图片

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 加密解密字符串
  3. android 字符串转json
  4. Android中的strings文件中字符串的拼接
  5. Android定义字符串数组资源并在程序中使用
  6. android字符串资源字符format
  7. 〖Android〗Android App项目资源字符串检查(检查是否缺少对应的翻
  8. android string.xml中格式资源字符串
  9. TabHost与RadioGroup结合完成的菜单【带效果图】5个Activity

随机推荐

  1. android timed gpio (linux 3.0.0) 受时
  2. 各种Layout用到的一些重要的属性
  3. layout_alignParentBottom失效
  4. Android纠正Activity横竖屏切换的生命周
  5. 转:Android(安卓)发送短信的方法
  6. Android界面布局开发使用的标签介绍
  7. android ScrollView边界阴影方法
  8. android 开发对gif解码(适配android 4.2、
  9. RealtiveLayout(相对布局属性)
  10. ListView 常用属性 详解