Android get programmatically created view's width

前端 未结 5 2114
一生所求
一生所求 2021-02-18 16:48

I have TextView created programmatically, like that:

                TextView mTextView = new TextView(getApplicationContext());

                final LayoutPar         


        
5条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-18 17:28

    I used this solution:

    yourView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    
        @Override
        public void onGlobalLayout() {
            // Ensure you call it only once
            yourView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
    
            // Here you can get the size :)
        }
    });
    

提交回复
热议问题