2个中文的空格就是:\t\t\t\t;空格:\n
单独设置字体大小:
s.setSpan(new ForegroundColorSpan(color), start, end,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
s.setSpan(new AbsoluteSizeSpan(58),start,end,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
以上2种效果,都会实现;
package com.imitate.shortvideo.master.utils;
import android.graphics.Typeface;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.StyleSpan;
import com.imitate.shortvideo.master.model.LineInfo;
import java.util.List;
/**
* author : jian11058
* time : 4/7/21
* description:
*/
TextView tv_hint = view.findViewById(R.id.dialog_declara_tv_hint);
String hint=mContext.getString(R.string.center_declara_content);
List<LineInfo> lineInfos=new ArrayList<>();
LineInfo lineInfo=new LineInfo();
lineInfo.setStartEnd(0,33);
lineInfos.add(lineInfo);
lineInfo=new LineInfo();
lineInfo.setStartEnd(60,113);//+35 +48
lineInfos.add(lineInfo);
lineInfo=new LineInfo();
lineInfo.setStartEnd(30,35);
lineInfos.add(lineInfo);
lineInfo=new LineInfo();
lineInfo.setStartEnd(169,210);
lineInfos.add(lineInfo);
SpannableString spannableString = TextColorUtils.setBoldSpan(hint, lineInfos);
tv_hint.setText(spannableString);
public class TextColorUtils {
public static SpannableString setBoldSpan(String tvCont, int start, int end) {
SpannableString spannableString = new SpannableString(tvCont);
StyleSpan styleSpan=new StyleSpan(Typeface.BOLD);
spannableString.setSpan(styleSpan, start, end, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
spannableString.setSpan(styleSpan, 20, 30, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
spannableString.setSpan(styleSpan, 40, 50, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
return spannableString;
}
public static SpannableString setBoldSpan(String tvCont, List<LineInfo> list) {
SpannableString spannableString = new SpannableString(tvCont);
for (LineInfo lineInfo : list) {
StyleSpan styleSpan=new StyleSpan(Typeface.BOLD);
spannableString.setSpan(styleSpan, lineInfo.getStartPoint(), lineInfo.getEndPoint(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
}
return spannableString;
}
}
package com.example.colormaterial.net.model;
import java.io.Serializable;
/**
* author : jian11058
* time : 4/7/21
* description:
*/
public class LineInfo implements Serializable {
private int startPoint;
private int endPoint;
public int getStartPoint() {
return startPoint;
}
public void setStartPoint(int startPoint) {
this.startPoint = startPoint;
}
public int getEndPoint() {
return endPoint;
}
public void setEndPoint(int endPoint) {
this.endPoint = endPoint;
}
public void setStartEnd(int start,int end){
startPoint=start;
endPoint=end;
}
}
//2组颜色设置
public static void setTextColorSpan(TextView textView, String tvCont, int color, int start, int end,int start2, int end2) {
SpannableString spannableString = new SpannableString(tvCont);
ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(color);
spannableString.setSpan(foregroundColorSpan, start, end, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
ForegroundColorSpan foregroundColorSpan2 = new ForegroundColorSpan(color);
spannableString.setSpan(foregroundColorSpan2, start2, end2, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
textView.setText(spannableString);
}
package com.superx.boost.clean.utils;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.BackgroundColorSpan;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StrikethroughSpan;
import android.text.style.URLSpan;
import android.text.style.UnderlineSpan;
import android.widget.TextView;
/**
* author : jian11058
* time : 2021/12/31
* desc :
*/
public class TextStyleUtils {
/**
* setTextColorSpan:字体色
* BackgroundColorSpan:背景色
* ClickableSpan:抽象类,可点击效果,重写onClick方法响应点击事件
* URLSpan:超链接
* MaskFilterSpan:EmbossMaskFilter浮雕效果,BlurMaskFilter模糊效果
* RelativeSpan:文字相对大小
* AbsoluteSpan:文字绝对大小
* ScaleXSpan:x轴缩放
* styleSpan:文字样式 StyleSpan styleSpan=new StyleSpan(Typeface.BOLD);
* TypefaceSpan:文字字体类型
* TextApearanceSpan:文字外貌
* UnderlineSpan:下划线
* StrikeThroughSpan:删除线
* SuperscriptSpan:上标
* SubscriptSpan:下标
* ImageSpan:图片
*/
public static void setTextColorSpan(TextView textView, String tvCont, int color, int start, int end) {
SpannableString spannableString = new SpannableString(tvCont);
ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(color);
spannableString.setSpan(foregroundColorSpan, start, end, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
ForegroundColorSpan foregroundColorSpan2 = new ForegroundColorSpan(color);
spannableString.setSpan(foregroundColorSpan2, 0, 3, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
textView.setText(spannableString);
}
/**
* 多组颜色设置
* @param textView
* @param tvCont
* @param color
* @param start
* @param end
* @param start2
* @param end2
*/
public static void setTextColorSpan(TextView textView, String tvCont, int color, int start, int end,int start2, int end2) {
SpannableString spannableString = new SpannableString(tvCont);
ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(color);
spannableString.setSpan(foregroundColorSpan, start, end, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
ForegroundColorSpan foregroundColorSpan2 = new ForegroundColorSpan(color);
spannableString.setSpan(foregroundColorSpan2, start2, end2, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
textView.setText(spannableString);
}
/**
* 下划线
*/
public static void addUnderLineSpan(TextView tv,String str) {
SpannableString spanString = new SpannableString(str);
UnderlineSpan span = new UnderlineSpan();
spanString.setSpan(span, 0, str.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.append(spanString);
}
public void setStrikethroughSpan(TextView textView, String tvCont, int start, int end) {
if (TextUtils.isEmpty(tvCont)){
return;
}
SpannableString spannableString = new SpannableString(tvCont);
StrikethroughSpan strikethroughSpan = new StrikethroughSpan();
spannableString.setSpan(strikethroughSpan, start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);
}
public void setAbsoluteSizeSpan(TextView textView, String tvCont, int AbsoluteSize, int start, int end) {
SpannableString spannableString = new SpannableString(tvCont);
AbsoluteSizeSpan absoluteSizeSpan = new AbsoluteSizeSpan(AbsoluteSize, true);
spannableString.setSpan(absoluteSizeSpan, start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);
}
public void setRelativeSizeSpan(TextView textView, String tvCont, float RelativeSize, int start, int end) {
SpannableString spannableString = new SpannableString(tvCont);
RelativeSizeSpan relativeSizeSpan = new RelativeSizeSpan(RelativeSize);
spannableString.setSpan(relativeSizeSpan, start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);
}
public static void setBackgroudColorSpan(TextView textView, String tvCont, int color, int start, int end) {
SpannableString spannableString = new SpannableString(tvCont);
BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan(color);
spannableString.setSpan(backgroundColorSpan, start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);
}
/**
* 多个点击事件,颜色;
*
* @param textView
* @param tvCont
* @param start
* @param end
* @param start2
* @param end2
* @param color
* @param clickableSpan
* @param clickableSpan2
*/
public static void setClickableSpan(TextView textView, String tvCont, int start, int end,int start2,int end2,int color,
ClickableSpan clickableSpan,ClickableSpan clickableSpan2) {
SpannableString spannableString = new SpannableString(tvCont);
// ClickableSpan clickableSpan = new ClickableSpan() {
// @Override
// public void onClick(View widget) {
// // Toast.makeText(MainActivity.this, "如果我是陈奕迅", Toast.LENGTH_SHORT).show();
// }
//
// @Override
// public void updateDrawState(TextPaint ds) {
// ds.setUnderlineText(false);
// }
// };
spannableString.setSpan(clickableSpan, start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
spannableString.setSpan(clickableSpan2, start2, end2, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
textView.setMovementMethod(LinkMovementMethod.getInstance());
if (color!=0){
ForegroundColorSpan colorSpan = new ForegroundColorSpan(color);
spannableString.setSpan(colorSpan, start, end, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
spannableString.setSpan(colorSpan, start2, end2, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
}
textView.setText(spannableString);
}
/**
* 单个字符串点击事件
*
* @param textView
* @param tvCont
* @param start
* @param end
* @param color
* @param clickableSpan
*/
public static void setClickableSpan(TextView textView, String tvCont, int start, int end,int color, ClickableSpan clickableSpan) {
SpannableString spannableString = new SpannableString(tvCont);
// ClickableSpan clickableSpan = new ClickableSpan() {
// @Override
// public void onClick(View widget) {
// // Toast.makeText(MainActivity.this, "如果我是陈奕迅", Toast.LENGTH_SHORT).show();
// }
//
// @Override
// public void updateDrawState(TextPaint ds) {
// ds.setUnderlineText(false);
// }
// };
spannableString.setSpan(clickableSpan, start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
textView.setMovementMethod(LinkMovementMethod.getInstance());
if (color!=0){
ForegroundColorSpan colorSpan = new ForegroundColorSpan(color);
spannableString.setSpan(colorSpan, start, end, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
}
textView.setText(spannableString);
}
public void setURLSpan(TextView textView, String tvCont, String url, int start, int end) {
SpannableString spannableString = new SpannableString(tvCont);
URLSpan urlSpan = new URLSpan(url);
spannableString.setSpan(urlSpan, start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setText(spannableString);
}
private static final String TAG="TextStyleUtils";
/**
* 字符串转换16进制
* */
public static String str2HexStr(String str) {
char[] chars = "0123456789ABCDEF".toCharArray();
StringBuilder sb = new StringBuilder("");
byte[] bs = str.getBytes();
int bit;
for (int i = 0; i < bs.length; i++) {
bit = (bs[i] & 0x0f0) >> 4;
sb.append(chars[bit]);
bit = bs[i] & 0x0f;
sb.append(chars[bit]);
sb.append(' ');
}
return sb.toString().trim().replace(" ","");
}
/**
* 判断是否有符号、空格
* 合法则true,不合法false
* */
public static boolean isLegal(String name){
if (TextStyleUtils.noContainsEmoji(name)){
return false;
}
// String str="~ !@#$%^&*()_+,./;'[],。、;‘《》?:“|{}<>?:|";
String str="~ !@#$%^&*()_+,-/;'[],。、;‘《》?:“|{}<>?:|";
for (int i=0;i<name.length();i++){
if (str.contains(name.substring(i,i+1))){
return false;
}
}
return true;
}
/**
* true为含表情
* */
private static boolean noContainsEmoji(String str) {//真为含有表情
int len = str.length();
for (int i = 0; i < len; i++) {
if (isEmojiCharacter(str.charAt(i))) {
return true;
}
}
return false;
}
private static boolean isEmojiCharacter(char codePoint) {
return !((codePoint == 0x0) ||
(codePoint == 0x9) ||
(codePoint == 0xA) ||
(codePoint == 0xD) ||
((codePoint >= 0x20) && (codePoint <= 0xD7FF)) ||
((codePoint >= 0xE000) && (codePoint <= 0xFFFD)) ||
((codePoint >= 0x10000) && (codePoint <= 0x10FFFF)));
}
}
单独设置,在调用的时候:
String content=tv_content.getText().toString();
int start=content.indexOf("《");
TextStyleUtils textStyleUtils = new TextStyleUtils();
int tvColor = getResources().getColor(R.color.blue);
textStyleUtils.setForegroundColorSpan(tv_content,content,tvColor,start,start+6);
批量设置文字大小、颜色、点击事件,设置点击之后字体背景色;
SpannableString spannableString = new SpannableString(content);
// spannableString.setSpan(new ForegroundColorSpan(tvColor), start,start+6, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
// spannableString.setSpan(new ForegroundColorSpan(tvColor), start+7,start+7+6, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
spannableString.setSpan(new ClickableSpan() {
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setColor(Color.BLUE); //设置文件颜色
ds.setUnderlineText(false); //设置下划线
}
@Override
public void onClick(@NonNull View widget) {
Log.e(TAG, "onClick: " );
}
},start,start+6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableString.setSpan(new ClickableSpan() {
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setColor(Color.BLUE); //设置文件颜色
ds.setUnderlineText(false); //设置下划线
}
@Override
public void onClick(@NonNull View widget) {
Log.e(TAG, "onClick2: " );
}
},start+7,start+7+6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
tv_content.setMovementMethod(LinkMovementMethod.getInstance());
tv_content.setHighlightColor(getResources().getColor(R.color.white));//设置点击之后的背景颜色;
tv_content.setText(spannableString);
一句话断句,为指定位置设置颜色;
String[] split = hint.split(",");
int tempStart = 0, tempEnd=0,tempSize=0;
int screenStart=0, screenEnd=0,screenSize=0;
int videoStart=0, videoEnd=0,videoSize=0;
int taskStart=0, taskEnd=0;
for (int i = 0; i < split.length; i++) {
String content = split[i];
switch (i){
case 0:
tempStart = 6;
if (content.length() == 10) {
tempEnd = 7;
} else {
tempEnd = 8;
}
tempSize=content.length();
break;
case 1:
screenStart = 0;
screenEnd = content.length();
screenSize=content.length();
break;
case 2:
if (content.length()==6){
videoEnd=1;
}else {
videoEnd=2;
}
videoSize=content.length();
break;
case 3:
taskStart=6;
if (content.length()==29){
taskEnd=7;
}else {
taskEnd=8;
}
break;
}
}
SpannableString spannableString = new SpannableString(hint);
ForegroundColorSpan spanTemlate = new ForegroundColorSpan(getResources().getColor(R.color.red_coin_color));
ForegroundColorSpan spanScreen = new ForegroundColorSpan(getResources().getColor(R.color.red_coin_color));
ForegroundColorSpan spanVideo = new ForegroundColorSpan(getResources().getColor(R.color.red_coin_color));
ForegroundColorSpan spanTask = new ForegroundColorSpan(getResources().getColor(R.color.red_coin_color));
spannableString.setSpan(spanTemlate, tempStart, tempEnd, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
// 标点符号的长度也的加上
spannableString.setSpan(spanScreen,tempSize+1+ screenStart, tempSize+1+screenEnd, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
int videoOffset=tempSize+screenSize+2;
spannableString.setSpan(spanVideo,videoOffset+videoStart,videoOffset+videoEnd,Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
int taskOffset=videoOffset+videoSize+1;
spannableString.setSpan(spanTask, taskOffset+taskStart, taskOffset+taskEnd, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
tvHint.setText(spannableString);