SpannableStringBuilder的使用方法

来源:互联网 发布:mac os x 10.12懒人包 编辑:程序博客网 时间:2024/05/18 01:37

SpannableStringBuilder的使用方法  

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);

    }

  
   
   
   
}

0 0
原创粉丝点击