废话不多说,先看效果图:
展开前:
展开后:
工具类:
/**
* 设置textView结尾...后面显示的文字和颜色
* @param context 上下文
* @param textView textview
* @param minLines 最少的行数
* @param originText 原文本
* @param endText 结尾文字
* @param endColorID 结尾文字颜色id
* @param isExpand 当前是否是展开状态
*/
public void toggleEllipsize(final Context context,
final TextView textView,
final int minLines,
final String originText,
final String endText,
final int endColorID,
final boolean isExpand) {
if (TextUtils.isEmpty(originText)) {
return;
}
textView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver
.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (isExpand) {
textView.setText(originText);
} else {
int paddingLeft = textView.getPaddingLeft();
int paddingRight = textView.getPaddingRight();
TextPaint paint = textView.getPaint();
float moreText = textView.getTextSize() * endText.length();
float availableTextWidth = (textView.getWidth() - paddingLeft - paddingRight) *
minLines - moreText;
CharSequence ellipsizeStr = TextUtils.ellipsize(originText, paint,
availableTextWidth, TextUtils.TruncateAt.END);
if (ellipsizeStr.length() < originText.length()) {
CharSequence temp = ellipsizeStr + endText;
SpannableStringBuilder ssb = new SpannableStringBuilder(temp);
ssb.setSpan(new ForegroundColorSpan(context.getResources().getColor
(endColorID)),
temp.length() - endText.length(), temp.length(),
Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
textView.setText(ssb);
} else {
textView.setText(originText);
}
}
if (Build.VERSION.SDK_INT >= 16) {
textView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
textView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
}
});
}
使用比较简单,参数列表都有说明。
例子:
boolean isExpandDescripe = false;// 初始展开状态为false,即未展开;
Onclick() {
...
// 给textView设置点击事件:
case R.id.tv_info_node_describe:
if (isExpandDescripe) {
isExpandDescripe = false;
tv_info_node_describe.setMaxLines(2);// 收起
} else {
isExpandDescripe = true;
tv_info_node_describe.setMaxLines(Integer.MAX_VALUE);// 展开
}
textViewSpanUtil.toggleEllipsize(context,
tv_info_node_describe, 2,
text,
"展开全部",
R.color.gray_discovery_ababab, isExpandDescripe);
break;
...
}