Solution for getting the width and height of the view in the OnCreate () 4 in the 0 method

Source: Internet
Author: User

very often when we create certain view dynamically, we need to determine the layout of the other view by getting their width and height, but the width and height of the view in OnCreate () get 0. The root cause of view.getwidth () and view.getheight () 0 is that the control has not finished drawing and you have to wait for the system to finish drawing the view before you can get it. This situation occurs when you need to use a dynamic layout (using wrap_content or match_parent). In general, there is no way to get the actual width of the view in the activity.oncreate (...)orOnresume () method. So, we have to use a workaround to get the width and height after the view is finished drawing. Here are some possible solutions.

1, monitoring Draw/layout event: Viewtreeobserver

Viewtreeobserver listens to many different interface drawing events. Generally speaking, Ongloballayoutlistener is where we can get to the width and height of the view. The code in the ongloballayout below is called after the view finishes the layout process.

1View.getviewtreeobserver (). Addongloballayoutlistener (NewViewtreeobserver.ongloballayoutlistener () {2 @Override3          Public voidongloballayout () { (NewRunnable () {5                  Public voidrun () {6View.getheight ();//height is ready7                 }8             });9         }Ten});

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)

2. Add a runnable to the layout queue: ()

This solution is my favorite, but almost no one knows this method. Simply put, just use () a runnable. The methods in the Runnable object are triggered after the view's measure, layout, and other events, and the specific reference Romain 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.

 1  final  View view=// SMTH;  2   ...  3 (new   Runnable () { 4   @Override  5  void   run () { 6  view.gethe Ight (); // height is ready  7   8

This method is better than viewtreeobserver :
1, your code will only be executed once, and you do not have to disable observer after each execution, much more worry.
2, the syntax is very simple


3, rewrite the view of the OnLayout method

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.

1View =NewView ( This) {2 @Override3     protected voidOnLayout (BooleanChangedintLintTintRintb) {4         Super. OnLayout (Changed, L, T, R, b);5View.getheight ();//height is ready6     }7};

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.

Attach: Get fixed width high

If the width and height of the view you want to get is fixed, then you can use it directly:

1 view.getmeasurewidth () 2 view.getmeasureheight ()

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.

Reference links

Solution for getting the width and height of the view in the OnCreate () 4 in the 0 method

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: 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.