Textview设置不同样式
来源:互联网 发布:海贼王 知乎 编辑:程序博客网 时间:2024/05/21 18:31
前言:在开发过程中会遇到,同一行文字不同属性,这时就可以利用这个工具类,设置你想要的属性啦!
用法:一个工具类,直接调用,效果图:
1.工具类:
/* Created by Administrator on 2016/7/8. */
public classStringFormatUtil {
private SpannableStringBuilder spBuilder;
private String wholeStr, highlightStr;
private Context mContext;
private int color;
private int start = 0, end = 0;public StringFormatUtil(Context context){ this.mContext=context;}/** * * @param wholeStr 全部文字 * @param highlightStr 改变颜色的文字 * @param color 颜色 * */public StringFormatUtil setTextColor(String wholeStr,String highlightStr,int color){ if(!TextUtils.isEmpty(wholeStr)&&!TextUtils.isEmpty(highlightStr)){ if(wholeStr.contains(highlightStr)){ /* * 返回highlightStr字符串wholeStr字符串中第一次出现处的索引。 */ start=wholeStr.indexOf(highlightStr); end=start+highlightStr.length(); }else{ return null; } }else{ return null; } spBuilder=new SpannableStringBuilder(wholeStr); color=mContext.getResources().getColor(color); CharacterStyle charaStyle=new ForegroundColorSpan(color); spBuilder.setSpan(charaStyle, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); return this;}/*设置背景*/public void setBackGround(String wholeStr,String highlightStr,int color){ if(!TextUtils.isEmpty(wholeStr)&&!TextUtils.isEmpty(highlightStr)){ if(wholeStr.contains(highlightStr)){ /* * 返回highlightStr字符串wholeStr字符串中第一次出现处的索引。 */ start=wholeStr.indexOf(highlightStr); end=start+highlightStr.length(); }else{ return; } }else{ return; } color=mContext.getResources().getColor(color); CharacterStyle charaStyle=new BackgroundColorSpan(color); spBuilder.setSpan(charaStyle, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);}/*字体*/public void setTypefaceSpan(String wholeStr,String highlightStr,String type){ if(!TextUtils.isEmpty(wholeStr)&&!TextUtils.isEmpty(highlightStr)){ if(wholeStr.contains(highlightStr)){ /* * 返回highlightStr字符串wholeStr字符串中第一次出现处的索引。 */ start=wholeStr.indexOf(highlightStr); end=start+highlightStr.length(); }else{ return; } }else{ return; } CharacterStyle charaStyle=new TypefaceSpan(type); spBuilder.setSpan(charaStyle, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);}/*设置缩放*/public void setScaleSpan(String wholeStr,String highlightStr,Float scal){ if(!TextUtils.isEmpty(wholeStr)&&!TextUtils.isEmpty(highlightStr)){ if(wholeStr.contains(highlightStr)){ /* * 返回highlightStr字符串wholeStr字符串中第一次出现处的索引。 */ start=wholeStr.indexOf(highlightStr); end=start+highlightStr.length(); }else{ return; } }else{ return; } CharacterStyle charaStyle=new ScaleXSpan(scal); spBuilder.setSpan(charaStyle, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);}/*字体格式*/public void setStyleSpan(String wholeStr,String highlightStr,int type){ if(!TextUtils.isEmpty(wholeStr)&&!TextUtils.isEmpty(highlightStr)){ if(wholeStr.contains(highlightStr)){ /* * 返回highlightStr字符串wholeStr字符串中第一次出现处的索引。 */ start=wholeStr.indexOf(highlightStr); end=start+highlightStr.length(); }else{ return; } }else{ return; } CharacterStyle charaStyle=new StyleSpan(type); spBuilder.setSpan(charaStyle, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);}public SpannableStringBuilder getResult(){ if (spBuilder != null) { return spBuilder; } return null;} }
2.直接调用:
String text = “[默认地址]天津工业大学 北苑 1号楼A区3011号楼A区\n” +
“3011号楼A区301”;StringFormatUtil spanStr = new StringFormatUtil(mContext); //设置字体颜色
spanStr.setTextColor(text,
“[默认地址]”, R.color.default_address_color); //设置背景颜色 spanStr.setBackGround(text,
“[默认地址]”, R.color.colorPrimary); //设置字体 spanStr.setTypefaceSpan(text,
“[默认地址]”, “monospace”); //设置字体缩放比例 spanStr.setScaleSpan(text,
“[默认地址]”, 2.0f); //设置字体样式,例如加粗、倾斜 spanStr.setStyleSpan(text,
“[默认地址]”, Typeface.BOLD_ITALIC); textView.setText(spanStr.getResult());
- TextView设置不同样式
- Textview设置不同样式
- 同一个textview中设置字体不同样式
- TextView文本设置不同的样式
- 同一个textview设置不同文字样式
- Android中对同一个TextView设置不同字体样式
- Android之同一个TextView设置不同样式的文字
- android小技巧之TextView设置不同字体大小及样式
- Android之同一个TextView设置不同样式的文字
- Android在一个TextView设置不同样式的文字
- Android布局TextView文字设置不同样式Spannable的使用
- TextView设置字体样式
- 设置TextView中样式
- android 一个textview不同样式
- 一个TextView设置不同大小字体、EditText显示两行不同样式hint
- android 设置TextView多行和单行各自显示不同的Gravity样式;textview.getLineCount()值为0解决
- TextView设置个别字体样式
- 设置textview部分字体样式
- Ubuntu16.4系统下为Python配置caffe环境
- c语言 可变参数 三个点 ...
- Linux互斥锁和条件变量
- AC自动机模板
- 4Sum、3Num
- Textview设置不同样式
- Win32字符串编码格式转化
- Zendstuido汉化方法
- 如何安装和配置Mantis
- 杭电5620
- Java跨域请求操作---封装类
- 【SQL】 DROP DELATE区别
- 线段树专题#1_蒟蒻训练历程记录_HDU1166
- java中图片简单操作