Can't get height of Widgets

前端 未结 2 1673
清歌不尽
清歌不尽 2021-01-24 02:05

I am facing a problem when gets height/width of TextView.

My code :

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(saved         


        
相关标签:
2条回答
  • 2021-01-24 02:19

    tv1.getHeight() will return 0 (as well as tv2.getWidth()) if it's not drawed yet.

    Solution would be to get the height after your view is layouted:

    tv1.post(new Runnable(){
        public void run(){
             int height = tv1.getHeight();
        }
    });
    tv2.post(new Runnable(){
        public void run(){
             int height = tv2.getHeight();
        }
    });
    
    0 讨论(0)
  • 2021-01-24 02:38

    View.getWidth()/View.getHeight() won't give you a meaningful answer until the view has been measured. Read about how android draws views here.
    See this thread to get an idea of when the measured dimensions will be available.

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