Android get programmatically created view's width

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

I have TextView created programmatically, like that:

                TextView mTextView = new TextView(getApplicationContext());

                final LayoutPar         


        
5条回答
  •  我在风中等你
    2021-02-18 17:35

    those values are constant value for FILL_PARENT and WRAP_CONTENT. If you want to check the view size you can try this way:

    tagMap.addView(mTextView);
    tagMap.post(new Runnable() {
                        @Override
                        public void run() {
                            mTextView. getWidth();
                        }
                    });
    

    you have to wait until android draws the TextView. This way you are posting a Runnable in the tagMap queue, that is executed, hopefully after the textview is draw (so it should be weight and height)

提交回复
热议问题