How to get number of lines of TextView?

前端 未结 10 2461
萌比男神i
萌比男神i 2020-11-28 09:29

I want to get the number of lines of a text view

textView.setText(\"Test line 1 Test line 2 Test line 3 Test line 4 Test line 5.............\")
相关标签:
10条回答
  • 2020-11-28 10:19

    As mentioned in this post,

    getLineCount() will give you the correct number of lines only after a layout pass.

    It means that you need to render the TextView first before invoking the getLineCount() method.

    0 讨论(0)
  • 2020-11-28 10:21

    Are you doing this onCreate? The Views aren't laid out yet, so getLineCount() is 0 for a while. If you do this later in the Window LifeCycle, you'll get your line count. You'll have a hard time doing it onCreate, but onWindowFocusChanged with hasFocus=true usually has the Views measured by now.

    The textView.post() suggestion is also a good one

    0 讨论(0)
  • 2020-11-28 10:23
     textView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
    
            @Override
            public boolean onPreDraw() {
                // Remove listener because we don't want this called before _every_ frame
                textView.getViewTreeObserver().removeOnPreDrawListener(this)
    
                // Drawing happens after layout so we can assume getLineCount() returns the correct value
                if(textView.getLineCount() > 2) {
                    // Do whatever you want in case text view has more than 2 lines
                }  
    
                return true; // true because we don't want to skip this frame
            }
    });
    
    0 讨论(0)
  • 2020-11-28 10:26

    ViewTreeObserver is not so reliable especially when using dynamic layouts such as ListView.

    Let's assume:
    1. You will do some work depending on the lines of TextView.
    2. The work is not very urgent and can be done later.

    Here is my solution:

    public class LayoutedTextView extends TextView {
    
            public LayoutedTextView(Context context) {
                    super(context);
            }
    
            public LayoutedTextView(Context context, AttributeSet attrs) {
                    super(context, attrs);
            }
    
            public LayoutedTextView(Context context, AttributeSet attrs, int defStyle) {
                    super(context, attrs, defStyle);
            }
    
            public interface OnLayoutListener {
                    void onLayouted(TextView view);
            }
    
            private OnLayoutListener mOnLayoutListener;
    
            public void setOnLayoutListener(OnLayoutListener listener) {
                    mOnLayoutListener = listener;
            }
    
            @Override
            protected void onLayout(boolean changed, int left, int top, int right,
                            int bottom) {
                    super.onLayout(changed, left, top, right, bottom);
    
                    if (mOnLayoutListener != null) {
                            mOnLayoutListener.onLayouted(this);
                    }
            }
    
    }
    

    Usage:

    LayoutedTextView tv = new LayoutedTextView(context);
    tv.setOnLayoutListener(new OnLayoutListener() {
            @Override
            public void onLayouted(TextView view) {
                    int lineCount = view.getLineCount();
                    // do your work
            }
    });
    
    0 讨论(0)
提交回复
热议问题