您的位置:首页 > 产品设计 > UI/UE

SpannableStringBuilder的使用方法

2015-12-21 18:01 429 查看
package com.example.helloworld;

import android.os.Bundle;

import android.app.Activity;

import android.graphics.Color;

//import android.text.Html;

import android.widget.TextView;

import android.text.Spannable;

import android.text.SpannableStringBuilder;

//import android.text.SpannableString;

//import android.text.Spanned;

import android.text.style.ForegroundColorSpan;

public class MainActivity extends Activity {

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

TextView tv=(TextView)findViewById(R.id.textview); --创建一个textview控件

String str="thisismyfirst class";--声明一个字符串变量

SpannableStringBuilder style=new SpannableStringBuilder(str); 将str字符串载入SpannableStringBuilder对象中

--分段显示str字符串的字体的颜色,ForegroundColorSpan(Color.RED)表示是1,到4这个范围内的字符的颜色是红色,

--SPAN_EXCLUSIVE_INCLUSIVE 表示1到4这个范围内的字符不包含第一个但是包含第4个字符,也就是从第二个字符开始到第四个字符,他的颜色都是红色

style.setSpan(new ForegroundColorSpan(Color.RED), 1, 4, Spannable.SPAN_EXCLUSIVE_INCLUSIVE); style.setSpan(new ForegroundColorSpan(Color.BLUE), 5, 7, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);

tv.setText(style);

}

}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: