Custom view is occasionally used in Android development, and in general, custom view needs to inherit the Onmeasure method of the view class, so why inherit the Onmeasure () function? Under what circumstances should inherit Onmeasure ()? What is the behavior of the system default Onmeasure () function? This article explores these questions.
First, we write a custom view that directly calls the system default Onmeasure function to see what happens:
package com.titcktick.customview;import android.content.context;import android.util.attributeset;import android.view.view;public class customview extends View { public customview (Context context) { super (context); } public customview (context context, attributeset attrs) { super (context, attrs); } @Override protected void onmeasure (Int widthmeasurespec, int heightmeasurespec) { super.onmeasure (WidthMeasureSpec , heightmeasurespec); }}
1. Parent controls use Match_parent,customview with Match_parent
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:orien tation= "vertical" > <com.titcktick.customview.customview android:layout_width= "match_parent" Android : layout_height= "match_parent" android:layout_margin= "10DP" android:background= "@android: Color/black"/>< ;/linearlayout>
This adds a margin of 10DP and sets the background of the view to black for easy identification of our CustomView, the effect is as follows:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/45/DE/wKioL1Pso77ROkIvAAAxTKcLymg465.jpg "style=" float: none; "title=" 1.png "alt=" Wkiol1pso77rokivaaaxtkclymg465.jpg "/>
As we can see, by default, CustomView fills the parent control if both the parent and CustomView use Match_parent.
2. Parent controls use Match_parent,customview with Wrap_content
Replace the CustomView layout_width/layout_height in the layout file with Wrap_content, and you will find that the result is still full of the parent control.
3. The parent control uses a fixed value with Match_parent,customview
Replace the CustomView layout_width/layout_heightin the layout file with 50DP, and you will find that the CustomView display results are 50DPX50DP:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/45/DC/wKiom1PsoqaShr74AAA8MgI5FPY164.jpg "style=" float: none; "title=" 2.png "alt=" Wkiom1psoqashr74aaa8mgi5fpy164.jpg "/>
4. The parent control uses a fixed value, customview using Match_parent or Wrap_content
So, if you replace the parent control's layout_width/layout_height with 50dp,customview set to Match_parent or Wrap_content, you will find that The CustomView display results are also for 50dpx50 DP.
5 Conclusion
If the custom CustomView takes the default Onmeasure function, the behavior is as follows:
(1) CustomView is set to match_parent or wrap_content without any difference, and its display size is determined by the parent control, which fills the space of the entire parent control.
(2) CustomView is set to a fixed value, its display size is the set value.
If your custom control's size calculation is consistent with the default behavior of the system, then you don't need to rewrite the onmeasure function.
6. How to write Onmeasure functions
The behavior of the system's default Onmeasure function is discussed here, and the following is how to rewrite the Onmeasure function, as well as the basic principle of onmeasure function, the key part in the code in the form of comments given, for reference only:
package com.titcktick.customview;import android.content.context;import android.util.attributeset;import android.view.view;public class customview extends view { private static final int default_view_width = 100; private static final int Default_view_height = 100; public customview ( Context context) { super (context); } public customview (context context, attributeset attrs ) { super (context, attrs); } @Override protected void onmeasure (Int widTHMEASURESPEC,&NBSP;INT&NBSP;HEIGHTMEASURESPEC) { int width = measuredimension (DEFAULT_VIEW_WIDTH , widthmeasurespec); int height = Measuredimension (Default_view_height, heightmeasurespec); setmeasureddimension (width, height); } protected int measuredimension ( int defaultSize, int measureSpec ) { int result = defaultSize; &nbSp; int specmode = measurespec.getmode (MeasureSpec); int specsize = measurespec.getsize (MeasureSpec); //1. layout gives a definite value, for example: 100DP&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;//2. layout is using match_parent, but the size of the parent control can be determined, such as setting a specific value or match_parent if (specmode == measurespec.exactly) { result = specsize; // Recommendation: Result directly using the OK value } //1. layout is using wrap_content //2. . Layout uses match_parent, but the parent control uses a deterministic value or WRAP_content else if (specmode == measurespec.at _most) { result = math.min (defaultSize, specSize); //Recommendation: Result cannot be greater than specsize } //unspecified, there are no restrictions, so you can set any size // In the case of a custom parent control, it is expected that the self-controlled part determines the size else { result = defaultsize; } return result; }}
this after overloading the Onmeasure function, you will find that when CustomView uses match_parent, it fills the entire parent control, and when CustomView uses Wrap_content, Its size is the default size defined in the code 100x100 pixels. of course, you can also rewrite the implementation of Measuredimension () according to your own needs.
The discussion about Onmeasure is introduced here, have any question welcome message or letter [email protected] exchange.
This article is from the "Shadow Three People" blog, please be sure to keep this source http://ticktick.blog.51cto.com/823160/1540134