In Android how to get the width of the Textview which is set to Wrap_Content

前端 未结 8 1645
时光取名叫无心
时光取名叫无心 2020-12-11 03:56

I am trying to add a text to the textview for which i have set the width as Wrap_content. I am trying to get the width of this textview. But its showing 0 in all the cases.

相关标签:
8条回答
  • 2020-12-11 04:41

    You can use this library to schedule the task of perform calculation on the width to the correct time after the view had been completely drawn

    https://github.com/Mohamed-Fadel/MainThreadScheduler

    Sample of use:

    MainThreadScheduler.scheduleWhenIdle(new Runnable() {
           @Override
           public void run() {
               int width = textview.getWidth();
               int height = textview.getHeight();
               textview.setText( String.valueOf( width +","+ height ));
           }
       });
    
    0 讨论(0)
  • 2020-12-11 04:47

    You should use this:

    textView.getMeasuredWidth();

    0 讨论(0)
提交回复
热议问题