Reference to:http://www.linuxidc.com/linux/2014-12/110164.htm
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:
PackageCom.titcktick.customview;ImportAndroid.content.Context;ImportAndroid.util.AttributeSet;ImportAndroid.view.View; Public classCustomViewextendsView { PublicCustomView (Context context) {Super(context); } PublicCustomView (Context context, AttributeSet attrs) {Super(context, attrs); } @Overrideprotected voidOnmeasure (intWidthmeasurespec,intHeightmeasurespec) { 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:orientation= "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:
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_height in the layout file with 50DP, and you will find that the CustomView display results are 50DPX50DP:
Http://www.linuxidc.com/Linux/2014-12/110164.htm
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. Http://www.linuxidc.com/Linux/2014-12/110164.htm
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:
PackageCom.titcktick.customview;ImportAndroid.content.Context;ImportAndroid.util.AttributeSet;ImportAndroid.view.View; Public classCustomViewextendsView {Private Static Final intDefault_view_width = 100; Private Static Final intDefault_view_height = 100; PublicCustomView (Context context) {Super(context); } PublicCustomView (Context context, AttributeSet attrs) {Super(context, attrs); } @Overrideprotected voidOnmeasure (intWidthmeasurespec,intHeightmeasurespec) { intwidth =measuredimension (Default_view_width, Widthmeasurespec); intHeight =measuredimension (Default_view_height, Heightmeasurespec); Setmeasureddimension (width, height); } protected intMeasuredimension (intDefaultSize,intMeasurespec) { intresult =defaultsize; intSpecmode =Measurespec.getmode (MEASURESPEC); intSpecsize =measurespec.getsize (MEASURESPEC); //1. Layout gives a definite value, for example: 100DP//2. Layout uses 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 uses the OK value } //1. Layout is using the 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-control part determines the size Else{result=defaultsize; } returnresult; }}
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.
[Android Pro] Android Development Practice: Why Inherit Onmeasure ()