4 ways to monitor the view measurement in Android activity

Source: Internet
Author: User
Tags message queue

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.