Last active
February 1, 2016 02:59
-
-
Save wutianlong/c129ca6507c6846531bd to your computer and use it in GitHub Desktop.
设置TextView最多显示多少行string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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会心的话庄白怎么选择啊?" | |
/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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