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

    }

  
   
   
   
}

發佈了41 篇原創文章 · 獲贊 1 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章