SpannableStringBuilder的使用方法
2016-08-03 18:21
459 查看
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控件
TextView tv=(TextView)findViewById(R.id.textview);
--声明一个字符串变量
String str="thisismyfirst class";
--将str字符串载入SpannableStringBuilder对象中
SpannableStringBuilder style=new SpannableStringBuilder(str);
style.setSpan(new ForegroundColorSpan(Color.RED), 1, 4, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
--分段显示str字符串的字体的颜色,ForegroundColorSpan(Color.RED)表示是1,到4这个范围内的字符的颜色是红色,
--SPAN_EXCLUSIVE_INCLUSIVE 表示1到4这个范围内的字符不包含第一个但是包含第4个字符,也就是从第二个字符开始到第四个字符,他的颜色都是红色
style.setSpan(new ForegroundColorSpan(Color.BLUE), 5, 7, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
tv.setText(style);
}
}
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控件
TextView tv=(TextView)findViewById(R.id.textview);
--声明一个字符串变量
String str="thisismyfirst class";
--将str字符串载入SpannableStringBuilder对象中
SpannableStringBuilder style=new SpannableStringBuilder(str);
style.setSpan(new ForegroundColorSpan(Color.RED), 1, 4, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
--分段显示str字符串的字体的颜色,ForegroundColorSpan(Color.RED)表示是1,到4这个范围内的字符的颜色是红色,
--SPAN_EXCLUSIVE_INCLUSIVE 表示1到4这个范围内的字符不包含第一个但是包含第4个字符,也就是从第二个字符开始到第四个字符,他的颜色都是红色
style.setSpan(new ForegroundColorSpan(Color.BLUE), 5, 7, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
tv.setText(style);
}
}
相关文章推荐
- SpannableStringBuilder的使用方法
- SpannableStringBuilder的使用方法
- SpannableStringBuilder的setSpan方法使用需注意点!
- SpannableStringBuilder的使用方法
- SpannableStringBuilder的使用方法
- EditText控件的使用续:Editor实现类SpannableStringBuilder实现的Spanned和Spannable接口方法
- 使用android SpannableStringBuilder实现图文混排,看到许多其他
- java学习之旅56--数组_StringBuilder和StringBuffer的使用_常用方法_方法链的实现_JDK源码分析
- 朝花夕拾:Java中String,StringBuffer,StringBuilder使用方法以及异同比较
- TextView SpannableString 使用方法
- 使用android SpannableStringBuilder实现图文混排
- 如何区分.Net中String和StringBuilder的使用方法
- TextView之SpannableStringBuilder使用总结
- StringBuilder使用方法
- 深入理解StringBuilder的使用方法
- java学习之旅57、58--数组_StringBuilder和StringBuffer的使用_JDK源码分析内部机制、常用方法补充_常见面试题答法
- StringBuilder使用方法
- StringBuilder使用方法
- 使用android SpannableStringBuilder实现图文混排,查看更多
- SpannableStringBuilder的使用