您的位置:首页 > 移动开发 > Android开发

android TextView 首行缩进与部分文字改变颜色大小效果

2018-01-05 10:42 656 查看
本文首发在我的个人博客: http://www.geekqian.com/post/21138b6f.html

转载请注明出处

TextView 首行伪缩进效果

// 原理, 使用Spannable 把前两个字设置为透明状态. 虽然是伪缩进, 但是不会因为分辨率的不同而产生各种问题, 注意如果
// TextView设置了可复制的话要再做处理, 否则会把透明的文字也复制了.

Spannable span = new SpannableString("缩进" + customerInfo.getDescription());

span.setSpan(new ForegroundColorSpan(Color.TRANSPARENT), 0, 2, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

mBusiness_scope.setText(span);


TextView 部分文字改变颜色大小效果

String msg = "一行中包含了特殊文字";
Spannable span = new SpannableString(msg);
String special = "特殊文字";
int start = msg.indexOf(special);
int end = start + special.length();
// 改变大小 (16为文字大小)
span.setSpan(new AbsoluteSizeSpan(dip2px(16)), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// 改变颜色 (Color.YELLOW 为文字颜色)
span.setSpan(new ForegroundColorSpan(Color.YELLOW), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
TextView.setText(span);

/**
* dip----to---px
* @return
*/
public static int dip2px(int dip) {
// 缩放比例(密度)
float density = getResources().getDisplayMetrics().density;
return (int) (dip * density + 0.5);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息