textview.getLineCount always 0 in android

前端 未结 6 1542
执笔经年
执笔经年 2020-11-30 04:28

I\'m trying to dynamically resize my textview but getlinecount() method always returns me 0 even after settext() and invalidate(). I\'m using the following code:



        
相关标签:
6条回答
  • 2020-11-30 05:03

    getLineCount() will give you the correct number of lines only after a layout pass. That means the TextView must have been drawn at least once. I assume that at this point of time your Textview is not drawn hence you are getting 0 as the line count

    0 讨论(0)
  • 2020-11-30 05:07

    It should be done on UI Thread by post method.

    textView.post(new Runnable() {
        @Override
        public void run() {
            Log.v("Line count: ", textView.getLineCount()+"");
        }
    });
    
    0 讨论(0)
  • 2020-11-30 05:16

    I know this question is quite old, but in case anyone comes here looking for the actual answer:

    holder.text2.setText(arr2[position]);
    holder.text2.post(new Runnable() {
        @Override
        public void run() {
            int lineCnt = holder.text2.getLineCount();
            // Perform any actions you want based on the line count here.
        }
    });
    
    0 讨论(0)
  • 2020-11-30 05:21

    In order to fix this issue apply the following lines:

    textView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            if (textView.getLineCount() > 1) {
                textView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        }
    });
    

    The OnGlobalLayoutListener will be called after every change to the TextView (after measuring it, drawing it, ..). Here you can catch changes to your TextView before it'll be drawn to the screen and do whatever you need with it.

    The last line in the code is to remove the listener, it's important since we don't want to continue catching each layout change.

    0 讨论(0)
  • 2020-11-30 05:21

    actually TextView.getLineCount() rely on TextView.mLayout.getLineCount but TextView.mLayout is lazy init before onMeasure, you can do like this:

           if (holder.contentTV.getLayout() == null) {
                holder.contentTV.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                    @Override
                    public boolean onPreDraw() {
                        holder.contentTV.getLineCount();
                        holder.contentTV.getViewTreeObserver().removeOnPreDrawListener(this);
                        return true;
                    }
                });
            } else {
                holder.contentTV.getLineCount();
            }
    
    0 讨论(0)
  • 2020-11-30 05:29

    I made an asynctask class and did my getLineCount()-related tasks in its onPostExecute method.

    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        new myAsyncTask().execute(null, null, null);
    }
    
    private class myAsyncTask extends AsyncTask<Void, Void, Void> {
    
        @Override
        protected Void doInBackground(final Void... params) {
            // It's okay to leave this as it is
            return null;
        }
    
        @Override
        protected void onPostExecute(final Void result) {
            super.onPostExecute(result);
            //DO YOUR TASK HERE, getLineCount(), etc.       
        }
    }
    
    0 讨论(0)
提交回复
热议问题