In development, you often encounter the need to get control size after activity initialization is complete.
However, this operation cannot be called in the OnCreate, Onresume, and other life cycle methods because we do not know when the view will be initialized to complete
To this end, a summary of the four implementation methods
1, activity/view#onwindowfocuschanged
This method is the listener focus pity Dorado, but in many cases it may be called multiple times and needs to be handled appropriately 2, View.post (runnable) This method is called only once, The idea is to put the custom runnable into the tail of the message queue, and when Looper calls to it, the view is initialized 3, viewtreeobserver This method is to listen for changes in the view tree, By calling its Ongloballayoutlistener interface, it can be called when the view tree changes, but may also be called multiple times 4, view.measure (int widthmeasurespec,int HEIGHTMEASURESPEC) This method is to manually measure the view to get a wide, complex, multi-case 1.match_parent Discard the measurement, because this is the parentsize of the parent container that needs to be known, we cannot get this value, so we cannot measure 2. Specific values (DP/PX) Specify values 100 int widthmeasurespec = Measurespec.makemeasurespec (Mea surespec.exactly); int heightmeasuerspec = MEASURESPEC.MAKEMEASURESPEC (100, measurespec.exactly); view.measure (Widthmeasurespec, Heightmeasurespec); 3.wrap_content following measure; int Widthmeasur Espec = Measurespec.makemEasurespec ((1<<30) -1,measurespec.at_most); int heightmeasurespec = Measurespec.makemeasurespec ((1<<30) -1,measurespec.at_most); View.measure ( WIDTHMEASURESPEC,HEIGHTMEASURESPEC);
4 ways to listen to view measurements in Android activity