咳咳,先来一张 简陋到新天际的 效果图
密恐的请自备氧气
废话少说 先上 原文链接
(以防万一: http://p.codekk.com/detail/Android/borjabravo10/ReadMoreTextView)
(依然以防万一:http://www.jcodecraeer.com/a/opensource/2016/0427/4187.html)
泡网的话 demo地址 404
首先起手式 Android Studio:
compile 'com.borjabravo:readmoretextview:2.0.1'
eclipse 的你心痛吗..
首先 xml 文件 values.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="accent'>#FF4081</color>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<declare-styleable name="ReadMoreTextView">
<attr name="trimExpandedText" format="string"/>
<attr name="trimCollapsedText" format="string"/>
<attr name="trimLength" format="integer"/>
<attr name="showTrimExpandedText" format="boolean"/>
<attr name="colorClickableText" format="color"/>
<attr name="trimLines" format="integer"/>
<attr name="trimMode">
<enum name="trimModeLine" value="0"/>
<enum name="trimModeLength" value="1"/>
</attr>
</declare-styleable>
<string name="read_less">收起</string>
<string name="read_more">展开</string>
</resources>
以上纯手打, 简书差评。
ok 下一步 创建 ReadMoreTextView 类 继承自 TextView
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.TextView;
public class ReadMoreTextView extends TextView {
private static final int TRIM_MODE_LINES = 0;
private static final int TRIM_MODE_LENGTH = 1;
private static final int DEFAULT_TRIM_LENGTH = 240;
private static final int DEFAULT_TRIM_LINES = 2;
private static final boolean DEFAULT_SHOW_TRIM_EXPANDED_TEXT = true;
private static final String ELLIPSIZE = "... ";
private CharSequence text;
private BufferType bufferType;
private boolean readMore = true;
private int trimLength;
private CharSequence trimCollapsedText;
private CharSequence trimExpandedText;
private ReadMoreClickableSpan viewMoreSpan;
private int colorClickableText;
private boolean showTrimExpandedText;
private int trimMode;
private int lineEndIndex;
private int trimLines;
public ReadMoreTextView(Context context) {
this(context, null);
}
public ReadMoreTextView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray typedArray = context.obtainStyledAttributes(attrs,
R.styleable.ReadMoreTextView);
this.trimLength = typedArray.getInt(
R.styleable.ReadMoreTextView_trimLength, DEFAULT_TRIM_LENGTH);
int resourceIdTrimCollapsedText = typedArray.getResourceId(
R.styleable.ReadMoreTextView_trimCollapsedText,
R.string.read_more);
int resourceIdTrimExpandedText = typedArray.getResourceId(
R.styleable.ReadMoreTextView_trimExpandedText,
R.string.read_less);
this.trimCollapsedText = getResources().getString(
resourceIdTrimCollapsedText);
this.trimExpandedText = getResources().getString(
resourceIdTrimExpandedText);
this.trimLines = typedArray.getInt(
R.styleable.ReadMoreTextView_trimLines, DEFAULT_TRIM_LINES);
this.colorClickableText = typedArray.getColor(
R.styleable.ReadMoreTextView_colorClickableText,
ContextCompat.getColor(context, R.color.accent));
this.showTrimExpandedText = typedArray.getBoolean(
R.styleable.ReadMoreTextView_showTrimExpandedText,
DEFAULT_SHOW_TRIM_EXPANDED_TEXT);
this.trimMode = typedArray.getInt(
R.styleable.ReadMoreTextView_trimMode, TRIM_MODE_LINES);
typedArray.recycle();
viewMoreSpan = new ReadMoreClickableSpan();
onGlobalLayoutLineEndIndex();
setText();
}
private void setText() {
super.setText(getDisplayableText(), bufferType);
setMovementMethod(LinkMovementMethod.getInstance());
setHighlightColor(Color.TRANSPARENT);
}
private CharSequence getDisplayableText() {
return getTrimmedText(text);
}
@Override
public void setText(CharSequence text, BufferType type) {
this.text = text;
bufferType = type;
setText();
}
private CharSequence getTrimmedText(CharSequence text) {
if (trimMode == TRIM_MODE_LENGTH) {
if (text != null && text.length() > trimLength) {
if (readMore) {
return updateCollapsedText();
} else {
return updateExpandedText();
}
}
}
if (trimMode == TRIM_MODE_LINES) {
if (text != null && lineEndIndex > 0) {
if (readMore) {
return updateCollapsedText();
} else {
return updateExpandedText();
}
}
}
return text;
}
private CharSequence updateCollapsedText() {
int trimEndIndex = text.length();
switch (trimMode) {
case TRIM_MODE_LINES:
trimEndIndex = lineEndIndex
- (ELLIPSIZE.length() + trimCollapsedText.length() + 1);
if (trimEndIndex < 0) {
trimEndIndex = trimLength + 1;
}
break;
case TRIM_MODE_LENGTH:
trimEndIndex = trimLength + 1;
break;
}
SpannableStringBuilder s = new SpannableStringBuilder(text, 0,
trimEndIndex).append(ELLIPSIZE).append(trimCollapsedText);
return addClickableSpan(s, trimCollapsedText);
}
private CharSequence updateExpandedText() {
if (showTrimExpandedText) {
SpannableStringBuilder s = new SpannableStringBuilder(text, 0,
text.length()).append(trimExpandedText);
return addClickableSpan(s, trimExpandedText);
}
return text;
}
private CharSequence addClickableSpan(SpannableStringBuilder s,
CharSequence trimText) {
s.setSpan(viewMoreSpan, s.length() - trimText.length(), s.length(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
return s;
}
public void setTrimLength(int trimLength) {
this.trimLength = trimLength;
setText();
}
public void setColorClickableText(int colorClickableText) {
this.colorClickableText = colorClickableText;
}
public void setTrimCollapsedText(CharSequence trimCollapsedText) {
this.trimCollapsedText = trimCollapsedText;
}
public void setTrimExpandedText(CharSequence trimExpandedText) {
this.trimExpandedText = trimExpandedText;
}
public void setTrimMode(int trimMode) {
this.trimMode = trimMode;
}
public void setTrimLines(int trimLines) {
this.trimLines = trimLines;
}
private class ReadMoreClickableSpan extends ClickableSpan {
@Override
public void onClick(View widget) {
readMore = !readMore;
setText();
}
@Override
public void updateDrawState(TextPaint ds) {
ds.setColor(colorClickableText);
}
}
private void onGlobalLayoutLineEndIndex() {
if (trimMode == TRIM_MODE_LINES) {
getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
ViewTreeObserver obs = getViewTreeObserver();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
obs.removeOnGlobalLayoutListener(this);
} else {
obs.removeGlobalOnLayoutListener(this);
}
refreshLineEndIndex();
setText();
}
});
}
}
private void refreshLineEndIndex() {
try {
if (trimLines == 0) {
lineEndIndex = getLayout().getLineEnd(0);
} else if (trimLines > 0 && getLineCount() >= trimLines) {
lineEndIndex = getLayout().getLineEnd(trimLines - 1);
} else {
lineEndIndex = getLayout().getLineEnd(
getLayout().getLineCount() - 1);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
好了 这样 可以直接使用该控件了(上图吧,心累)