本文共 978 字,大约阅读时间需要 3 分钟。
本文首发在我的个人博客:
转载请注明出处
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);}