Resolves 4 ways to get a view's width and height of 0 in the OnCreate () process __java/android

Source: Internet
Author: User

Very often when we create some view dynamically, we need to determine the layout of the other view by obtaining their width and height, but OnCreate () gets the width and height of the view to get 0.view.getwidth () and View.getheight () is the root cause of 0 because the control hasn't finished drawing yet, 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). Generally speaking, in activity.oncreate (...), Onresume () method, there is no way to get the actual height of view. So, we have to use a workaround to wait until the view is finished to get the width and height. Here are some possible solutions. 1, monitor Draw/layout event: Viewtreeobserver

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

1 View.getviewtreeobserver (). Addongloballayoutlistener (New Viewtreeobserver.ongloballayoutlistener () {
 2         @Override
 3 public         void Ongloballayout () {
 4             mscrollview.post (new Runnable () {
 5 public                 void Run () {
 6                     view.getheight ();//height is ready
 7                 }
 8             });
 9         }
10});

Note, however, that this method is invoked every time the layout of some view changes (for example, a view is set to invisible), so after getting the width you want, remember to remove the Ongloblelayoutlistener:

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

Used in the SDK LVL >= 16 O'Clock
public void Removeongloballayoutlistener (Viewtreeobserver.ongloballayoutlistener victim) 2, Add a runnable to the layout queue: View.post ()

This solution is my favorite, but few people know this method. To put it simply, just use View.post () a runnable. The methods in the Runnable object will be triggered after the view's measure, layout, and so on, specific references to Romain Guy:

The UI event queues handle events sequentially. After the Setcontentview () is invoked, the event queue contains a message that requires a layout, so anything you post to the queue will be executed after the layout has changed.

1 final View view=//smth;
2.. 3 View.post (New Runnable () {
4             @Override
5 public             void Run () {
6                 view.getheight ();//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 observer after each execution to disable, more worry.
2, the grammar is very simple


Reference:
http://stackoverflow.com/a/3602144/774398
http://stackoverflow.com/a/3948036/774398

3, rewrite the view of the OnLayout method

This method is only useful in certain scenarios, such as when you are going to perform something that should be internalized and modular in view, or the solution will seem tedious and cumbersome.

1 view = new View (this) {
2     @Override
3     protected void OnLayout (Boolean changed, int l, int t, int r, int b) {
4         super.onlayout (changed, L, T, R, b);
5         view.getheight ();//height is ready
6     }
7};

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 obtain are fixed, you can use it directly:

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

Note, however, that the width and height obtained by these two methods may be different from the actual draw. The official document explains the different reasons:

The size of the view is determined by the width and height. A view actually has two kinds of 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 by 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 when it is drawn on the screen and layout is completed. These values may differ from measure width and height. Width and height can be obtained by getwidth () and getheight.

Reference Links

https://stackoverflow.com/questions/3591784/getwidth-and-getheight-of-view-returns-0/24035591#24035591

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.