Onmeasure and onlayout

Source: Internet
Author: User

 


329 reading comments (0)
Favorites
Report, we know that the view is displayed on the screen first through measure and layout. when calling the onmeasure (INT widthspec, int heightspec) method, it involves the use of measurespec. measurespec has three modes: unspecified, exactly, and at_most, so what is the relationship between these modes and the layout parameter fill_parent and wrap_content we usually set. After code testing, we know that when we set the width or height to fill_parent, the container calls the measure method of the sub-view during layout and the input mode is exactly, because the sub-view occupies the space of the remaining container, its size is determined. When wrap_content is set, the container transmits at_most,
Indicates the maximum size of a sub-view, so that the view will set its own size based on the upper limit. When the size of the sub-view is set to an exact value, the container imports exactly, while the unspecified mode of measurespec does not yet find the applicable conditions.

The onmeasure method of view defaults to the minimum size of mminwidth (usually 0) or background drawable when the mode is unspecified. When the mode is exactly or at_most, the size is set to the size of the input measurespec.

To correct this idea, the fill_parent should be a sub-view that will occupy the space of the remaining containers, rather than overwrite other view spaces that have already been laid out, of course, there is no space to allocate for the layout subview. Therefore, the fill_parent attribute is very important to the layout sequence. What I previously thought was to fill up all the container space. No wonder Google changed the fill_parent name to match_parent in version 2.2.

We will discuss the android custom view framework today. For regular games, we need to solve the following problems in view:
1. control events
2. Refresh the view
3. Draw a view. (document/Android Development Network)
1. For control events today, we only process the key event onkeydown. In future articles, we will discuss how to deal with the ontouchevent and sensor gravity sensing on screen touch.
2. The methods for refreshing the view are as follows:
Invalidate (int l, int T, int R, int B) refreshes the local disk. The four parameters are left, top, right, and bottom.
Refresh invalidate () for the entire view ()
Refresh a rectangle (rect dirty)
Refresh a feature drawableinvalidatedrawable (drawable)
When the invalidate class is executed, the view is set to invalid, and the ondraw method is called again.
Because today's view is relatively simple, android123 prompts you to use the postinvalidate method in thread to refresh the thread in addition to the handler method.
3. Draw a view mainly through the canvas of the form parameter in ondraw (). The related drawings mainly include drawrect, drawline, and drawpath.
The view method also overwrites many interfaces. The callback method can help us determine the position and size of the view, such as onmeasure (INT, INT) called to determine the size requirements for this view and all of its children. , Onlayout (Boolean, Int, INT) called when this view shoshould assign a size and position to all of its children
And onsizechanged (INT, Int, Int, INT) called when the size of this view has changed. You can use logcat to obtain the changes of each form parameter when the view changes.

Both functions are called by the system. onmeasure calculates the width and height of the view, and onlayout determines the layout and position.

I encountered the problem of display and refresh the joint viewgroup interface.

Question 1: dynamically add components (for example, textview)
Description: You can dynamically create a new view (textview) in oncreate and add it to the viewgroup to display the view normally. If I change to a button click event and click Add a component (for example, textview), it is found that the first addition does not respond, the viewgroup does not change, and the second operation is normal. I have observed that in onlayout (Boolean arg0, int arg1, int arg2, int arg3, int arg4), arg4 = 0, and the height of the entire viewgroup is 0, so there is no change.

Excuse me: why is the height of viewgroup 0 for the first time? How can this problem be solved.

Question 2: add N components (such as textview and button) to the viewgroup. The logic is to click a component, and the component gone drops and rearranges the entire view. However, the view disappears, leave a hole .. The entire view is not rearranged according to the logic in onlayout.

Could you tell me how to dynamically refresh the page of viewgroup. (Although the gone is refreshed, it is not the effect I want)

I have solved the second problem. Although it is troublesome in my business, the theory is still relatively simple.

For example, the control list is listview <textview> list...

1. Delete the textview: List. Remove (TV );
2. Remove all child views: viewgroup. removeallviews ();
3. Reload the subview: viewgroup. Add (TV );
This solves the problem that the onlayout method cannot be refreshed.

However, I still wonder why the above problem 1 will not occur in this case, not because the control has been initialized, Because I later found that although the control has been initialized, however, when multiple lines are added in one west line, the viewgroup is still the original size.

  

We know that the view displayed on the screen must first go through measure and layout. when calling the onmeasure (INT widthspec, int heightspec) method, it involves the use of measurespec. measurespec has three modes: unspecified, exactly, and at_most, so what is the relationship between these modes and the layout parameter fill_parent and wrap_content we usually set. After code testing, we know that when we set the width or height to fill_parent, the container calls the measure method of the sub-view during layout and the input mode is exactly, because the sub-view occupies the space of the remaining container, its size is determined. When wrap_content is set, the container transmits at_most,
Indicates the maximum size of a sub-view, so that the view will set its own size based on the upper limit. When the size of the sub-view is set to an exact value, the container imports exactly, while the unspecified mode of measurespec does not yet find the applicable conditions.

The onmeasure method of view defaults to the minimum size of mminwidth (usually 0) or background drawable when the mode is unspecified. When the mode is exactly or at_most, the size is set to the size of the input measurespec.

To correct this idea, the fill_parent should be a sub-view that will occupy the space of the remaining containers, rather than overwrite other view spaces that have already been laid out, of course, there is no space to allocate for the layout subview. Therefore, the fill_parent attribute is very important to the layout sequence. What I previously thought was to fill up all the container space. No wonder Google changed the fill_parent name to match_parent in version 2.2.

We will discuss the android custom view framework today. For regular games, we need to solve the following problems in view:
1. control events
2. Refresh the view
3. Draw a view. (document/Android Development Network)
1. For control events today, we only process the key event onkeydown. In future articles, we will discuss how to deal with the ontouchevent and sensor gravity sensing on screen touch.
2. The methods for refreshing the view are as follows:
Invalidate (int l, int T, int R, int B) refreshes the local disk. The four parameters are left, top, right, and bottom.
Refresh invalidate () for the entire view ()
Refresh a rectangle (rect dirty)
Refresh a feature drawableinvalidatedrawable (drawable)
When the invalidate class is executed, the view is set to invalid, and the ondraw method is called again.
Because today's view is relatively simple, android123 prompts you to use the postinvalidate method in thread to refresh the thread in addition to the handler method.
3. Draw a view mainly through the canvas of the form parameter in ondraw (). The related drawings mainly include drawrect, drawline, and drawpath.
The view method also overwrites many interfaces. The callback method can help us determine the position and size of the view, such as onmeasure (INT, INT) called to determine the size requirements for this view and all of its children. , Onlayout (Boolean, Int, INT) called when this view shoshould assign a size and position to all of its children
And onsizechanged (INT, Int, Int, INT) called when the size of this view has changed. You can use logcat to obtain the changes of each form parameter when the view changes.

Both functions are called by the system. onmeasure calculates the width and height of the view, and onlayout determines the layout and position.

I encountered the problem of display and refresh the joint viewgroup interface.

Question 1: dynamically add components (for example, textview)
Description: You can dynamically create a new view (textview) in oncreate and add it to the viewgroup to display the view normally. If I change to a button click event and click Add a component (for example, textview), it is found that the first addition does not respond, the viewgroup does not change, and the second operation is normal. I have observed that in onlayout (Boolean arg0, int arg1, int arg2, int arg3, int arg4), arg4 = 0, and the height of the entire viewgroup is 0, so there is no change.

Excuse me: why is the height of viewgroup 0 for the first time? How can this problem be solved.

Question 2: add N components (such as textview and button) to the viewgroup. The logic is to click a component, and the component gone drops and rearranges the entire view. However, the view disappears, leave a hole .. The entire view is not rearranged according to the logic in onlayout.

Could you tell me how to dynamically refresh the page of viewgroup. (Although the gone is refreshed, it is not the effect I want)

I have solved the second problem. Although it is troublesome in my business, the theory is still relatively simple.

For example, the control list is listview <textview> list...

1. Delete the textview: List. Remove (TV );
2. Remove all child views: viewgroup. removeallviews ();
3. Reload the subview: viewgroup. Add (TV );
This solves the problem that the onlayout method cannot be refreshed.

However, I still wonder why the above problem 1 will not occur in this case, not because the control has been initialized, Because I later found that although the control has been initialized, however, when multiple lines are added in one west line, the viewgroup is still the original size.

  

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.