Skip to content

Instantly share code, notes, and snippets.

@wutianlong
Last active February 1, 2016 02:59
Show Gist options
  • Save wutianlong/c129ca6507c6846531bd to your computer and use it in GitHub Desktop.
Save wutianlong/c129ca6507c6846531bd to your computer and use it in GitHub Desktop.
设置TextView最多显示多少行string
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/user_block"
android:ellipsize="none"
tools:text="请问69会心的话庄白怎么选择啊?请问69会心的话庄白怎么选择啊?请问69会心的话庄白怎么选择啊?请问69会心的话庄白怎么选择啊?"
/>
public static void makeEllipseEnd(TextView textView, String str, int maxLine) {
textView.setText(str);
textView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
textView.getViewTreeObserver().removeOnPreDrawListener(this);
if (textView.getLineCount() > maxLine) {
int offset = textView.getLayout().getLineEnd(maxLine - 1);
String text = textView.getText().subSequence(0, offset).toString();
if (text.endsWith("\n")) {
text = text.subSequence(0, offset - 1) + "…\n";
} else {
text = text.subSequence(0, offset - 1) + "…";
}
textView.setText(text);
return false;
}
return true;
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment