how to use getlinecount() in textview android

前端 未结 6 1773
忘掉有多难
忘掉有多难 2021-01-12 02:03

i want to know how many line in my text view. i already set mytextview text, then i want to get how many line it take in mytextview.

i use mytextview.getLineCount()

6条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-12 02:43

    The Following will provide the lines count of the textview at the same place you set tv.setText().

    int maxTextViewWidth="ENTER MAX WIDTH HERE";
    
    tv.setText("hello\nhow are you?");
    
    int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(maxTextViewWidth, View.MeasureSpec.AT_MOST);
    
    int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    
    tv.measure(widthMeasureSpec, heightMeasureSpec);
    
    int lineCount=tv.getLineCount();
    

提交回复
热议问题