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

前端 未结 8 1668
时光取名叫无心
时光取名叫无心 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:32

    hello use this method:

    textview.post(new Runnable() {
        @Override
        public void run() {
            int width = textview.getWidth();
            int height = textview.getHeight();
            textview.setText( String.valueOf( width +","+ height ));
        }
    

    });

    source: https://gist.github.com/omorandi/59e8b06a6e81d4b8364f

提交回复
热议问题