Android gets a high-width view of the way

Source: Internet
Author: User

One, MeasureSpec ( measuring method ):


LinearLayout newstoplayout = (linearlayout) viewhashmapobj.get ("Top"), int w = View.MeasureSpec.makeMeasureSpec (0, View.MeasureSpec.UNSPECIFIED); int h = View.MeasureSpec.makeMeasureSpec (0,view.measurespec.unspecified); Newstoplayout.measure (W, h); int height = newstoplayout.getmeasuredheight ();

Note, however, that the width and height obtained by these two methods may not be the same as after the actual draw. The official documentation explains the different reasons:

The size of the view is determined by width and height. A view actually has two width and height values at the same time. The first type is measure width and measure height. They define how much width and height The view wants to occupy in the parent view (see layout for details). Measured height and width can be obtained through getmeasuredwidth () and Getmeasuredheight (). The second type is width and height, sometimes called drawing width and drawing height. These values define the actual size of the view after drawing on the screen and when layout is complete. These values may differ from measure width and height. Width and height can be obtained by getwidth () and getheight.

TwoaddOnGlobalLayoutListener增加整体布局监听):

View.getviewtreeobserver (). Addongloballayoutlistener (    new Viewtreeobserver.ongloballayoutlistener () {         @ Override public         void Ongloballayout () {              int width =view.getmeasuredwidth ();             int height =view.getmeasuredheight ();          }});

note, however, that this method is called every time the layout of some view changes (for example, a view is set to invisible), so when you get the desired width, remember to remove the Ongloblelayoutlistener:

Used in the SDK LVL < 16 o'clock: public void Removeglobalonlayoutlistener (Viewtreeobserver.ongloballayoutlistener victim)

Used in SDK LVL >= 16 o'clock: public void Removeongloballayoutlistener (Viewtreeobserver.ongloballayoutlistener victim)

Third, View.post ():

View.post (New Runnable () {             @Override public             void Run () {                 view.getheight ();             }});
just use View.post () a runnable. The methods in the Runnable object are triggered after the view's measure, layout, and other events, and the specific referenceRomain Guy:

The UI event queue processes events sequentially. After Setcontentview () is called, the event queue contains a messagethat requires re-layout, so anything you post to the queue will be executed after the layout has changed. Your code executes only once, and you don't have to disable observer after each execution, and it's much more hassle-free.

OnLayout method of Rewriting view:

view = new View (this) {     @Override     protected void OnLayout (Boolean changed, int l, int t, int r, int b) {         super . OnLayout (Changed, L, T, R, b);         View.getheight ();      } };
This method is useful only in certain scenarios, such as when you want to perform something that should be internalized as his inner logic, modularized in view, the solution is very lengthy and cumbersome. It is important to note that the OnLayout method is called many times, so consider what to do in this method, or disable your code after the first execution.


Android gets a high-width view of the way

Related Article

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.