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);
}
}
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);
}
}
相关文章推荐
- SAP BW QUERY VARIABLES (BW增强变量)
- 12.真机UI调试神器Reveal的使用
- IOS开发之----设置UITableView背景色和选中背景色
- Error: Installation error: INSTALL_FAILED_UID_CHANGED
- Oozie中Hive action配置时的注意事项
- easyui防止重复提交及进度条报错解决: Cannot read property 'onClose' of undefined
- iOS 9: UIStackView入门
- Oozie配置Hue
- 随机排序12宫格的头像展示Demo
- EasyUI dataGrid 翻页时会把之前页选中的记录取消掉
- iOS UISearchController的使用
- UItableView的自适应
- C# 队列(Queue)基础
- 29.Which two statements are true regarding constraints?
- mysql 连接url中useUnicode=true&characterEncoding=UTF-8 的作用
- 11.使用NSUserDefaults存储存储
- CEGUI0.8.4引入到自己工程中
- UIAlertView ios9 开始废弃 使用UIAlertController弹框提示
- 【ANT】构建文件build.xml
- 10.使用UITableView制作永久性存储的信息存储