通过上一篇文章

我的Android进阶之旅------>
Android在TextView中显示图片方法

(地址:http://blog.csdn.net/ouyang_peng/article/details/46916963)

我们学会了在TextView中显示图片的方法,如今我们来学习怎样为TextView组件中显示的文本加入背景色。要求完毕的样子如图所看到的:

首先来学习使用BackgroundColorSpan对象设置文字背景色。代码例如以下:

  1. TextView textView=(TextView) findViewById(R.id.myTextView);
  2. //要显示的字符串
  3. String text="带背景色的文字";
  4. //将字符串转换为SpannableString对象
  5. SpannableString spannableString=new SpannableString(text);
  6. //确定要设置的字符串的start和end
  7. int start=0;
  8. int end=7;
  9. //创建BackgroundColorSpan对象。指定背景色为黄色
  10. BackgroundColorSpan backgroundColorSpan=new BackgroundColorSpan(Color.YELLOW);
  11. //使用setSpan方法将指定字符串转换成BackgroundColorSpan对象
  12. spannableString.setSpan(backgroundColorSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  13. //用SpannableString对象设置TextView
  14. textView.setText(spannableString);

BackgroundColorSpan仅仅能设置文字的背景色,为了更加通用,自己定义一个ColorSpan类,能够同一时候设置文字颜色和背景色。代码例如以下:

  1. package com.oyp.edittext;
  2. import android.text.TextPaint;
  3. import android.text.style.CharacterStyle;
  4. public class ColorSpan extends CharacterStyle {
  5. private int mTextColor;
  6. private int mBackgroundColor;
  7. public ColorSpan(int textColor,int backgroundColor){
  8. mTextColor=textColor;
  9. mBackgroundColor=backgroundColor;
  10. }
  11. //覆盖CharacterStyle类的updateDrawState方法
  12. //并在该方法中设置了文字和背景颜色
  13. @Override
  14. public void updateDrawState(TextPaint tp) {
  15. tp.bgColor=mBackgroundColor;
  16. tp.setColor(mTextColor);
  17. }
  18. }

在ColorSpan类中实现了CharacterStyle的updateDrawState方法。

该方法在系统開始绘制要设置样式的字符串之前调用,以便改动绘制文字的属性,比如:文字颜色、背景颜色等。当中TextPaint是Paint的子类。Paint类用于描写叙述绘制的属性。如画笔的颜色、画笔的粗细等。如今我们同事使用BackgroundColorSpan和ColorSpan类设置文字和背景颜色,代码例如以下:

  1. package com.oyp.edittext;
  2. import android.os.Bundle;
  3. import android.text.Spannable;
  4. import android.text.SpannableString;
  5. import android.text.style.BackgroundColorSpan;
  6. import android.widget.TextView;
  7. import android.app.Activity;
  8. import android.graphics.Color;
  9. public class MainActivity extends Activity {
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.oyp);
  14. TextView textView=(TextView) findViewById(R.id.myTextView);
  15. //要显示的字符串
  16. String text="<没有背景><黄色背景>\n\n<蓝色背景,红色文字>";
  17. //将字符串转换为SpannableString对象
  18. SpannableString spannableString=new SpannableString(text);
  19. //确定要设置的字符串的start和end
  20. int start=6;
  21. int end=12;
  22. //创建BackgroundColorSpan对象。指定背景色为黄色
  23. BackgroundColorSpan backgroundColorSpan=new BackgroundColorSpan(Color.YELLOW);
  24. //使用setSpan方法将指定字符串转换成BackgroundColorSpan对象
  25. spannableString.setSpan(backgroundColorSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  26. /**
  27. * <蓝色背景,红色文字> 子字符串的開始位置(没一个"\n"算一个长度)
  28. * 因为该子字符串再原字符串的最好,因此,end对于字符串的长度。也就是text.length()
  29. */
  30. start=14;
  31. //创建ColorSpan对象
  32. ColorSpan colorSpan=new ColorSpan(Color.RED, Color.BLUE);
  33. //将指定文字转换成ColorSpan对象
  34. spannableString.setSpan(colorSpan, start, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  35. //用SpannableString对象设置TextView
  36. textView.setText(spannableString);
  37. }
  38. }

oyp.xml

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:paddingBottom="@dimen/activity_vertical_margin"
  6. android:paddingLeft="@dimen/activity_horizontal_margin"
  7. android:paddingRight="@dimen/activity_horizontal_margin"
  8. android:paddingTop="@dimen/activity_vertical_margin"
  9. tools:context=".MainActivity" >
  10. <TextView
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:id="@+id/myTextView"
  14. />
  15. </RelativeLayout>

程序执行效果例如以下图所看到的:



                            ====================================================================================

  作者:欧阳鹏  欢迎转载,与人分享是进步的源泉!

  转载请保留原文地址:http://blog.csdn.net/ouyang_peng

====================================================================================

 

我的Android进阶之旅------> Android为TextView组件中显示的文本加入背景色的更多相关文章

  1. 我的Android进阶之旅------> Android为TextView组件中显示的文本添加背景色

    通过上一篇文章 我的Android进阶之旅------> Android在TextView中显示图片方法 (地址:http://blog.csdn.net/ouyang_peng/article ...

  2. 我的Android进阶之旅------&gt; Android为TextView组件中显示的文本加入背景色

    通过上一篇文章 我的Android进阶之旅------> Android在TextView中显示图片方法 (地址:http://blog.csdn.net/ouyang_peng/article ...

  3. 我的Android进阶之旅------> Android在TextView中显示图片方法

    面试题:请说出Android SDK支持哪些方式显示富文本信息(不同颜色.大小.并包含图像的文本信息),并简要说明实现方法. 答案:Android SDK支持如下显示富文本信息的方式. 1.使用Tex ...

  4. 我的Android进阶之旅------>Android颜色值(#AARRGGBB)透明度百分比和十六进制对应关系以及计算方法

    我的Android进阶之旅-->Android颜色值(RGB)所支持的四种常见形式 透明度百分比和十六进制对应关系表格 透明度 十六进制 100% FF 99% FC 98% FA 97% F7 ...

  5. 我的Android进阶之旅------>Android中查看应用签名信息

    一.查看自己的证书签名信息 如上一篇文章<我的Android进阶之旅------>Android中制作和查看自定义的Debug版本Android签名证书>地址:http://blog ...

  6. 我的Android进阶之旅------>Android利用温度传感器实现带动画效果的电子温度计

    要想实现带动画效果的电子温度计,需要以下几个知识点: 1.温度传感器相关知识. 2.ScaleAnimation动画相关知识,来进行水印刻度的缩放效果. 3.android:layout_weight ...

  7. 我的Android进阶之旅------>Android实现用Android手机控制PC端的关机和重启的功能(三)Android客户端功能实现

    我的Android进阶之旅------>Android实现用Android手机控制PC端的关机和重启的功能(一)PC服务器端(地址:http://blog.csdn.net/ouyang_pen ...

  8. 我的Android进阶之旅------>Android疯狂连连看游戏的实现之实现游戏逻辑(五)

    在上一篇<我的Android进阶之旅------>Android疯狂连连看游戏的实现之加载界面图片和实现游戏Activity(四)>中提到的两个类: GameConf:负责管理游戏的 ...

  9. 我的Android进阶之旅------>Android疯狂连连看游戏的实现之加载界面图片和实现游戏Activity(四)

    正如在<我的Android进阶之旅------>Android疯狂连连看游戏的实现之状态数据模型(三)>一文中看到的,在AbstractBoard的代码中,当程序需要创建N个Piec ...

随机推荐

  1. Python GUI——tkinter菜鸟编程(中)

    8. Radiobutton 选项按钮:可以用鼠标单击方式选取,一次只能有一个选项被选取. Radiobutton(父对象,options,-) 常用options参数: anchor,bg,bitm ...

  2. 【电商】性能测试网站搭建:XAMPP1.8+DBShop1.3

    1.安装准备 1.1软件版本 XAMPP的版本:XAMPP 1.8.2 DBShop的版本:DBShop 1.3   1.2.安装环境 我的环境:win10 win7,win10都可以运行的,安装步骤 ...

  3. mysql 聚集函数 count 使用详解

    mysql 聚集函数 count 使用详解 本文将探讨以下问题 1.count(*) . count(n).count(null)与count(fieldName) 2.distinct 与 coun ...

  4. 2017蓝桥杯购物单(C++B组)

    原题: 标题: 购物单 小明刚刚找到工作,老板人很好,只是老板夫人很爱购物.老板忙的时候经常让小明帮忙到商场代为购物.小明很厌烦,但又不好推辞.这不,XX大促销又来了!老板夫人开出了长长的购物单,都是 ...

  5. 10.2 io流 之字节流和字符流

    FileWriter 用于写入字符流.要写入原始字节流,请考虑使用 FileOutputStream. io流相关文档: https://www.cnblogs.com/albertrui/p/836 ...

  6. python3(二十六)slots

    """ """ __author__ = 'shaozhiqi' # python是动态语言,所以定义类后,我们可以动态的给类绑定属性和方法 ...

  7. 解决xcode ***is missing from working copy

    这是由于SVN置顶文件导致的,cd 至项目根目录 命令行 输入 find . -type d -name .svn | xargs rm -rf

  8. AJ学IOS(20)UI之UIPickerView_点菜系统

    AJ分享,必须精品 先看效果图 ## UIPickerView控件 UIPickerView用处: 用来展示很多行(row) 很多列(component )的数据,多用于电子商务的点菜,城市选择等等. ...

  9. 分布式 and 集群

    集群是个物理形态,强调个体和群体之间的联系: 同一个业务部署在多个服务器上,形成的逻辑上的整体. 分布式是个工作方式.强调请求和处理直接的分发状况: 一个业务分拆多个子业务,部署在不同的服务器上,通过 ...

  10. 落谷 P1734 最大约数和

    题目描述 选取和不超过S的若干个不同的正整数,使得所有数的约数(不含它本身)之和最大. 输入格式 输入一个正整数S. 输出格式 输出最大的约数之和. 输入输出样例 输入 #1复制 11 输出 #1复制 ...