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

前端 未结 8 1650
时光取名叫无心
时光取名叫无心 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 ));
           }
       });
    

提交回复
热议问题