问题
I want to check the no of lines in my text view from the string I am going to pass to the test view. I have added android:maxLines = "2".... If it exceeds two i need to show/hide a view....How can I achieve this?
回答1:
Use a thread to count the number of lines,
textView.setText("Text Here");
textView.post(new Runnable() {
@Override
public void run() {
Log.v("Line count: ", textView.getLineCount()+"");
}
});
If you want to limit the number of lines in TextView from xml then use android:maxLines
回答2:
I believe the TextView has a method getLineCount which you can use to get the number of lines. You can compare that function's return value with maxLines and show/hide if the limit is reached.
See also: TextView.getLine()
And: TextView - maxLines attribute
来源:https://stackoverflow.com/questions/22854554/how-to-check-the-number-of-lines-in-a-string-in-android