SoFunction
Updated on 2025-03-01

Solution to the problem of invalid use of ellipsize attribute in android TextView multi-line text (more than 3 lines)

TextView property in layout file

Copy the codeThe code is as follows:

<TextView
android:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:lineSpacingMultiplier="1.0"
android:lines="6"
android:text="@string/agrinbusiness_content"
android:textColor="#7f7f7f"
android:textSize="13sp" />

 

Control the number of lines displayed in the JAVA code

Copy the codeThe code is as follows:

ViewTreeObserver observer = (); //textAbstract is a TextView control
(new OnGlobalLayoutListener() {

@Override
public void onGlobalLayout() {
ViewTreeObserver obs = ();
(this);
if(() > 6) //Change when the number of rows is greater than
{
int lineEndIndex = ().getLineEnd(5); //Set the ellipsis on the sixth line
String text = ().subSequence(0, lineEndIndex-3) +"...";
(text);
}
}
});