通过上一篇文章

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

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

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

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

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

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

package com.oyp.edittext;

import android.text.TextPaint;
import android.text.style.CharacterStyle; public class ColorSpan extends CharacterStyle {
private int mTextColor;
private int mBackgroundColor; public ColorSpan(int textColor,int backgroundColor){
mTextColor=textColor;
mBackgroundColor=backgroundColor;
}
//覆盖CharacterStyle类的updateDrawState方法
//并在该方法中设置了文字和背景颜色
@Override
public void updateDrawState(TextPaint tp) {
tp.bgColor=mBackgroundColor;
tp.setColor(mTextColor);
}
}

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

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

package com.oyp.edittext;

import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.BackgroundColorSpan;
import android.widget.TextView;
import android.app.Activity;
import android.graphics.Color; public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.oyp); TextView textView=(TextView) findViewById(R.id.myTextView);
//要显示的字符串
String text="<没有背景><黄色背景>\n\n<蓝色背景,红色文字>";
//将字符串转换为SpannableString对象
SpannableString spannableString=new SpannableString(text);
//确定要设置的字符串的start和end
int start=6;
int end=12;
//创建BackgroundColorSpan对象。指定背景色为黄色
BackgroundColorSpan backgroundColorSpan=new BackgroundColorSpan(Color.YELLOW);
//使用setSpan方法将指定字符串转换成BackgroundColorSpan对象
spannableString.setSpan(backgroundColorSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
/**
* <蓝色背景,红色文字> 子字符串的開始位置(没一个"\n"算一个长度)
* 因为该子字符串再原字符串的最好,因此,end对于字符串的长度。也就是text.length()
*/
start=14;
//创建ColorSpan对象
ColorSpan colorSpan=new ColorSpan(Color.RED, Color.BLUE);
//将指定文字转换成ColorSpan对象
spannableString.setSpan(colorSpan, start, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//用SpannableString对象设置TextView
textView.setText(spannableString);
}
}

oyp.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/myTextView"
/>
</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. 【STM32项目笔记】STM32CubeMX+Keil+Proteus联合实现LED闪烁

    摘要 利用STM32CubeMx配置STM32芯片的功能,然后将配置后的内容生成代码,并导出成可以使用Keil打开编辑的文件,在Keil中添加控制代码后,下载到Proteus仿真中,使用仿真观察代码执 ...

  2. 在Android Studio中导入jar包

    #1 下载jar包文件, #2 拷贝到libs目录下 #3 打开你的build.gradle,在dependencies加入如下代码 dependencies {compile files('libs ...

  3. Jmeter压力测试笔记(6)性能调测-压力并发-模拟生产环境数据

    问题原因找到了,那就好办了. 找到阿里云技术人员,让他们强行给我们上架了一个共享代理模式的Redis. 并重新进行压力测试. 哦豁~ 开心,压力测试顺利,异常率大大降低实际为: 数据库DBA反馈,数据 ...

  4. Linux服务器架设篇,DNS服务器(三),正反解区域的配置

    一.大体架构 DNS服务器其实只有一个"真正"的配置文件,即 /etc/named.conf .其他的配置文件都是依据此配置展开的.每个域都需要两个配置文件,即正解文件和反解文件. ...

  5. iOS岗位招聘标准水涨船高,五年iOS程序员表示面试太难了

    人才济济的iOS开发者,你凭什么脱颖而出? 与岗位要求相去甚远,如何挑战极限? 想去心怡公司,如何马到成功? 那么,你的绝招是什么呢? 在这个iOS岗位供不应求的市场,对iOS开发者对要求日益增长,面 ...

  6. idle中上传jar包并使用的方法

    创建一个lib目录,将jar包拉到该目录下. 需要导入的Jar包上,点击右键,选择Add as Library…

  7. windows上jmeter目录结构功能

    1.bin :存储了jmeter的可执行程序,如启动 2.lib:存储了jmeter的整合的功能(如.jar文件程序) 3.启动jmeter:双击bin\apachejmeter.jar jmeter ...

  8. python如何操作excel 基础代码

    一 基础操作1打开excel表格并获取表格名称 wookbook = load_workbook(filename = 表格文件路径) (注意只能打开存在的表格,不能用该方法创建一个新表格文件) wo ...

  9. tomcat通过tomcat 安装根目录下的conf-Catalina-localhost目录发布项目详解

    tomcat通过conf-Catalina-localhost目录发布项目详解   Tomcat发布项目的方式大致有三种,但小菜认为通过在tomcat的conf/Catalina/localhost目 ...

  10. 数据结构和算法(Golang实现)(26)查找算法-哈希表

    哈希表:散列查找 一.线性查找 我们要通过一个键key来查找相应的值value.有一种最简单的方式,就是将键值对存放在链表里,然后遍历链表来查找是否存在key,存在则更新键对应的值,不存在则将键值对链 ...